Re: member variables in python

2006-03-31 Thread Kent Johnson
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"
> is there a better way to access this if we go with what you mentioned
> in another class ..

1. Tell BB about the AA instance when it is created:

class BB:
   def __init__(self, aa):
 self.aa = aa

   def method(self):
 if self.aa.testflag:

2. Pass the flag to BB.method() directly:

   def method(self, testflag):
 if testflag:

Kent
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: member variables in python

2006-03-31 Thread bruno at modulix
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 '[EMAIL PROTECTED]'.split('@')])"
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: member variables in python

2006-03-31 Thread PyPK
I see that.But here in my case the testflags is computed inside the
member function something like

class AA:

def __init__(self):
self.test_flag = 0 # initialize

def methods(self, value):
save_value = _munge(value)
self.test_flag = save_value

Now basically I want use this self.test_flag in another class ..
Basically I wanted to know if there is a better way than what i did
before..not necessarly a member variable way ..just looking for a more
eligant way if there is any..

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: member variables in python

2006-03-31 Thread Fredrik Lundh
"PyPK" wrote:

> hi how do I write this better with member variables rather than global
> as you see below.
>
> eg:
>
> test-flag = 0
>
> class AA:
>def __init__(...):
>
>   def methos(self,...):
>global test-flag
>test-flag = xx
>
> instead of something like above ..how do i put it i terms of member
> variables?

you mean something like

class AA:

def __init__(self):
self.test_flag = 0 # initialize

def methods(self, value):
self.test_flag = value

# ...

aa = AA()
aa.methods(1)
print aa.test_flag

?





-- 
http://mail.python.org/mailman/listinfo/python-list


Re: member variables in python

2006-03-31 Thread Grant Edwards
On 2006-03-31, PyPK <[EMAIL PROTECTED]> wrote:

> hi how do I write this better with member variables

Sorry, I don't know what "member variables" are.

> rather than global as you see below.

What you did below isn't global.  It's scope is limited to the
module containing the class.  If you got something that's used
by multiple classes in the module, then module-scope seems like
the logical choice.

> eg:
>
> test-flag = 0
>
> class AA:
>def __init__(...):
>
>   def methos(self,...):
>global test-flag
>test-flag = xx
>
> instead of something like above ..how do i put it i terms of member
> variables?

Dunno.  What are "member varibles"?

-- 
Grant Edwards   grante Yow!  Don't SANFORIZE me!!
  at   
   visi.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: member variables in python

2006-03-31 Thread PyPK
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"
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 ..

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: member variables in python

2006-03-31 Thread Kent Johnson
PyPK wrote:
> hi how do I write this better with member variables rather than global
> as you see below.
> 
> eg:
> 
> test-flag = 0
> 
> class AA:
>def __init__(...):
> 
>   def methos(self,...):
>global test-flag
>test-flag = xx
> 
> instead of something like above ..how do i put it i terms of member
> variables?

class AA:
def __init__(...):
   self.test_flag = 0

   def methos(self,...):
self.test_flag = xx

Note 'test-flag' is not a valid name.

Kent
-- 
http://mail.python.org/mailman/listinfo/python-list


member variables in python

2006-03-31 Thread PyPK
hi how do I write this better with member variables rather than global
as you see below.

eg:

test-flag = 0

class AA:
   def __init__(...):

  def methos(self,...):
   global test-flag
   test-flag = xx

instead of something like above ..how do i put it i terms of member
variables?

-- 
http://mail.python.org/mailman/listinfo/python-list