"configuring" a class

James Stroud jstroud at ucla.edu
Wed Feb 22 22:00:07 EST 2006


Russ wrote:
> I would like to let the user of one of my classes "configure" it by
> activating or de-activating a particular behavior (for all instances of
> the class).
> 
> One way to do this, I figured, is to have a static class variable,
> along with a method to set the variable. However, I am stumped as to
> how to do that in python. Suggestions welcome.
> 
> The next best thing, I figure, is to just use a global variable.
> Several "methods" of the class then check the value of the global
> variable to determine what to do. The user can then just set the
> variable to get the desired behavior.
> 
> However, I tried this and it does not seem to work. I imported the
> class, then set the global variable. But the new value of the variable
> somehow did not get back into the class methods that need to see it.
> 
> Can anyone give me a clue about this? If there is a better way, please
> let me know. Thanks.
> 

py> class C:
...   doit = 2
...
py> C.doit
2
py> a = C()
py> b = C()
py> c = C()
py> a.doit, b.doit, c.doit
(2, 2, 2)
py> C.doit = 5
py> a.doit, b.doit, c.doit
(5, 5, 5)
py> b.doit = 13
py> a.doit, b.doit, c.doit
(5, 13, 5)

James



More information about the Python-list mailing list