I defined my class foo as:
class Foo(object):
# static member variables of the parent class Foo
is_logged = False
is_domain_set = False
def __init__(self, username, password):
# do something here.
def login(self, login_url):
# do the login ...
if self.login_successful():
self.is_logged = True
self.is_domain_set = True
# Output `True` as expected
self.log.info("is_logged: %s" % self.is_logged)
self.log.info("is_domain_set: %s" % self.is_domain_set)
return
class Bar(Foo):
# Foo is the parent class of Bar
super(Bar, self).__init__(username, password)
# Both are outputting `False` when it should be `True`
self.log.info("is_logged: %s" % self.is_logged)
self.log.info("is_domain_set: %s" % self.is_domain_set)
if not self.is_logged:
self.login(login_url)
Then they are used in another class, Baz:
class Baz(oject):
def __init__(self, username, password, login):
# -- not important for the problem.
self.foo = Foo(username, password)
# login for auth purpose
if not self.foo.is_logged:
self.foo.login(login_url)
# Now use some of the objects of the `Bar` class, which
# will do its job:
self.bar = Bar(username, password)
The program flow is that simple. Foo is used for some basic operations like the login, while Bar is used to some other specific operations. However, I expect static variables to be shared between the base class (one per instance of the class), and it's child class. The intention is to have is_logged and is_domain_set to behave like a Singleton. What am I doing wrong?
The apparent behavior is that there's a static variable for Foo and one for Bar, even though Bar inherits from Foo. However, What am I trying to accomplish is different. I want Foo and Bar to share the same static variable. One option (bad) is to have a global variable, but I'm trying to do without this.
is_logged_in? Can you explain your use case? This makes no sense to me.is_logged_inwithout using global variables. Makes sense?Fooalready authenticated the is logged in, thenBardoesn't need to authenticate. I think the example I gave is clear about that. I only care about if it's logged or not.