1

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
  • Your question is a little confused - the attributes are accessible to anything that has access to the object. The scope of the object is the issue here. Commented Dec 3, 2012 at 19:52

1 Answer 1

4

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.)

Sign up to request clarification or add additional context in comments.

4 Comments

It's also worth noting that this is terribly bad design and will come back to haunt you. It's not a good idea to do this.
@Lattyware: can you explain why is it a terribly bad design?
@JohnJ The issue is that it's rare you actually want one of something, and even if you do, you often later find you want to produce another of it to use elsewhere. The behaviour of a global can be replaced with good passing of data, and will make your classes easier to move around and reuse.
@BrenBarn: Thanks a lot for your answer. So, it wasnt anything funny I was doing - basically, if an object is declared within a global scope, its(and its attributes are) available everywhere - is this the bottom line?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.