Static member variables

Thomas Svensson thomas_s at ebox.tninet.se
Mon Jul 10 16:16:38 EDT 2000


In article <8kahq3$fof$1 at slb0.atl.mindspring.net>, aahz at netcom.com (Aahz
Maruch) wrote:
[...]
> You can have class variables, but they're not exactly what I'd call
> "static" and they certainly aren't constant.
> 
> class foo:
>   bar = 2
>   def __init__(self):
>     self.baz = 3
> 
> f = foo()
> 
> f.bar is a class variable; f.baz is an instance variable.  Note that you
> can run into problems if you're not careful because of the way scoping
> rules work.

Thanks,

Very logical when I see it now. About trouble, do you mean something
similar to this:

class X:
    foo = 'bar'
class Y(X):
    pass

print X.foo, Y.foo

X.foo = 'duh'
print X.foo, Y.foo


It yields this for me somewhat surprising output:

bar bar
duh duh

Assigning to Y.foo instead gives a different output. I'm not quite
sure I understand why, have to read the manual first.

-- 
Thomas




More information about the Python-list mailing list