470,604 Members | 2,121 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,604 developers. It's quick & easy.

member variables in python

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?

Mar 31 '06 #1
7 1802
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
Mar 31 '06 #2
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 ..

Mar 31 '06 #3
On 2006-03-31, PyPK <su*******@gmail.com> 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
Mar 31 '06 #4
"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

?

</F>

Mar 31 '06 #5
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..

Mar 31 '06 #6
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 'o****@xiludom.gro'.split('@')])"
Mar 31 '06 #7
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
Mar 31 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by Scott Brady Drummonds | last post: by
3 posts views Thread by Sambo | last post: by
1 post views Thread by mangalalei | last post: by
17 posts views Thread by lm401 | last post: by
8 posts views Thread by David Veeneman | last post: by
7 posts views Thread by Valeriu Catina | last post: by
7 posts views Thread by Immortal Nephi | last post: by
13 posts views Thread by Henri.Chinasque | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.