0

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.

5
  • 1
    You want one global setting for is_logged_in? Can you explain your use case? This makes no sense to me. Commented Nov 2, 2015 at 19:10
  • @Two-BitAlchemist: I want them to share is_logged_in without using global variables. Makes sense? Commented Nov 2, 2015 at 19:12
  • So that only one user can be logged in at a time? I doubt that's the best way to do it if so. Commented Nov 2, 2015 at 19:14
  • @Two-BitAlchemist. No. If Foo already authenticated the is logged in, then Bar doesn't need to authenticate. I think the example I gave is clear about that. I only care about if it's logged or not. Commented Nov 2, 2015 at 19:16
  • I'm with Two-Bit Alchemist here; static variables by definition are shared across all instances, so if one user is logged in, they all are. Commented Nov 2, 2015 at 19:21

1 Answer 1

2

You might have defined "static" variables (in Python they're usually known as class attributes) for is_logged and is_domain_set, but you immediately overwrite them with instance attributes when you assign to self.is_logged.

If you wanted to assign to the class variables only, you would need to do self.__class__.is_logged = True.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.