Global variables within classes.

Donn Ingle donn.ingle at gmail.com
Sat Nov 10 10:00:13 EST 2007


> The first creates an attribute of the class, the second creates an
> attribute in the instance.

Given that, can you clarify this:

class Test:
    attribute = "original value"
    
class Bob:
    def __init__(self):
        self.ref = Test()
        
class Jim:
    def __init__(self):
        self.ref = Test()
        
b = Bob()
j = Jim()

print b.ref.attribute #prints "original value"

b.ref.attribute = "haschanged"
## Where is the value "haschanged" going here?
print b.ref.attribute # print "haschanged"

print j.ref.attribute #prints "original value"
## If it changed and an attribute of the Class, then
## why is it back to "original value" ?

> Actually the lookup is somewhat more complex than that when you use new-
> style classes (and you should always use new-style classes): properties
> (or other objects with a 'get' method) are class attributes but take
> precedence over instance attributes.
What is a 'get' method? Do you mean it should get all Java-esque with
getBlah and setBlah for every little thing?

I have been hearing about "new classes" for a while but there's no clarity
in the Python docs (that I can find). Have you any references for me? I
thought they were the standard by now anyway. I've only been pythoning
since 2.3 anyway.


\d




More information about the Python-list mailing list