Global variables within classes.
Hrvoje Niksic
hniksic at xemacs.org
Tue Nov 27 18:21:38 EST 2007
Kevac Marko <mkevac at gmail.com> writes:
> When changing default value, is there any way to change class
> attribute and all referenced attributes too?
>
> class M:
> name = u"Marko"
>
> a, b = M(), M()
> a.name = u"Kevac"
> print M.name, a.name, b.name
> -> Marko Kevac Marko
>
> Is there any way to get here -> Kevac Kevac Kevac ?
> Or what is the right way of keeping global variables in classes?
Simply assign to M.name instead of to a.name. Assigning to a.name
creates an a-specific attribute that overrides the global one.
If you insist on being able to assign to a.name to change M.name, look
up "descriptors" -- but that feels like fighting the language.
More information about the Python-list
mailing list