member variables in python

bruno at modulix onurb at xiludom.gro
Fri Mar 31 11:34:03 EST 2006


PyPK wrote:
> ok I reason I was going with globals is that i use this variable in
> another class something like this along with above
> 
> testflag = 0
> 
> class AA:
>    def __init__(...):
> 
>   def methos(self,...):
>        global testflag
>        testflag = xx
> 
> class BB:
>     def __init__(...):
> 
>    def method2(..):
>        if testflag:
>            print "do this"

Seems that you're looking for a logging package (hint: there's one in
the standard lib).

> is there a better way to access this if we go with what you mentioned
> in another class ..
> I wanted to avoid globals thats the reason i am looking for better
> solution ..
> 

A general solution would be a combination of
1/ a Singleton or Monostate (for storing application-wide settings) and
2/ a method decorator managing additionnal responsabilities according to
the values of 1.

There are examples of Singleton/Borg/Monostate/whatnot in the Python
cookbook and elsewhere - as usual, google is your friend - and a Q&D
example of decorator doing this kind of jobs in the decorator library
http://wiki.python.org/moin/PythonDecoratorLibrary

My 2 cents
-- 
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'onurb at xiludom.gro'.split('@')])"



More information about the Python-list mailing list