Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8953865
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T14:11:41+00:00 2026-06-15T14:11:41+00:00

I am trying to learn OO Python, and I have a doubt regarding the

  • 0

I am trying to learn OO Python, and I have a doubt regarding the scope of object attributes once it has been instantiated. So, I decided to write a very simple program:

class Struct:
    def __init__(self):
        self.resadd=[]
        self.ressub=[]
        self.resmul=[]

class MathStruct:
    def mathss(self,x,y):
        add=x+y
        sub=x-y
        mul=x*y
        resstruct.resadd.append(add)
        resstruct.ressub.append(sub)
        resstruct.resmul.append(mul)

if __name__=='__main__':
    resstruct=Struct()
    for i in range(10,20):
        mathsstuff=MathStruct()
        mathsstuff.mathss(i,5)
    print len(resstruct.resadd),resstruct.resadd
    print len(resstruct.ressub),resstruct.ressub
    print len(resstruct.resmul),resstruct.resmul

As you can see, the program is quite straightforward – but, here is my question – I instantiate the “result” object using:
resstruct=Struct()
and then use the resstruct.resadd, resstruct.ressub and resstruct.resmul attributes within the “mathss” function. Is this legal(I get the right answer!)? Does this mean that the object attributes are available even within other functions?

Normally, I would return values from the function and then append it to the object attribute, but I was (pleasantly) surprised to see that I can directly modify the object attribute within another function.

..In a nutshell, my question is what is the scope of the object attributes once it is instantiated? Sorry if this is a 101 question.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T14:11:42+00:00Added an answer on June 15, 2026 at 2:11 pm

    The line restruct = Struct() occurs at the module top level, outside any function or class, so it creates an object in the module global namespace. This namespace is, as the term implies, global to the module, and can be accessed from all functions. If you had done restruct = Struct() inside a function, it wouldn’t be global and would disappear when the function finished.

    In other words, if you had created it in a function scope, the object’s lifetime would be limited to that scope. But you created it in global scope, so it lives forever. (An object’s attributes have no scope of their own as such; they are inside that object’s own instance namespace and are accessible only via the object. You can access obj.attr whenever you can access obj.)

    (Note, though, that assigning to global variables from inside a function requires a global statement. You are able to access restruct because you only read the value of the variable and mutate that object with methods. If you tried to assign to restruct within a function, you would be creating a new local variable shadowing the global one.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been trying to learn python over the last couple of months and
I have a question. I have been really trying to learn Python. For a
I have been trying to learn Python for a while now. By chance, I
Hi im trying to learn Python and have a err i can understand ,
I am trying to learn how Python reloads modules, but have hit a roadblock.
I've been trying to learn about metaclasses in Python. I get the main idea,
I'm trying to learn python and have encountered some strange behaviour. I am experimenting
I am trying to learn python and I have no clue why the last
Trying to learn Regex in Python to find words that have consecutive vowel-consonant or
I am trying to learn Django (and Python) and have created my own app

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.