member variables in python

P

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?
 
K

Kent Johnson

PyPK said:
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
 
P

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 ..
 
G

Grant Edwards

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"?
 
F

Fredrik Lundh

PyPK said:
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

?

</F>
 
P

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..
 
B

bruno at modulix

PyPK said:
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
 
K

Kent Johnson

PyPK said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top