0

Are class variables essentially global variables?

Consider this example, in bar.py:

class Bar(object):
    b = {}

then in foo.py:

def foo1():
    import bar
    bar.Bar.b["key"] = "foo1"


def foo2():
    import bar
    print bar.Bar.b.get("key", "foo2")

foo1()
foo2()

The "key" value is persistently "foo1."

For my purposes this seems like a global variable. But why isn't the class variable being reset or deleted between the calls to foo1 and foo2?

1
  • @mgilson I am asking why isn't the variable being garbage collected between the two calls; that link seems to be talking about the difference between the instance and class variables. Commented Mar 13, 2014 at 15:58

1 Answer 1

3

It's a class attribute. In your case, the dict is bound to the class Bar. Everytime you import bar, you get the same class bar.Bar (after all, it is in the bar's global namespace), and consequentially, you get the same dict bar.Bar.b.

If you want each Bar instance to have a different dict, you need to make an instance:

class Bar(object):
    def __init__(self):
        self.b = {}

And then:

def foo1():
    import bar
    bar.Bar().b["key"] = "foo1"


def foo2():
    import bar
    print bar.Bar().b.get("key", "foo2")

foo1()
foo2()

As far as why the module bar isn't being garbage collected, when you import something, python stores a reference to that module (see sys.modules). The next time you import that module, python picks out the proper item from sys.modules and gives you a reference to it. There are a few reasons for this.

  • Efficiency -- Why re-evaluate code you've already evaluated?
  • Sane behavior

Imagine a module with a special value:

# mod.py
CONSTANT = object()

now you want to use that in foo.py and bar.py.

# bar.py
import mod
def bar():
    return mod.CONSTANT

# foo.py
import mod
import bar
if bar.bar() is mod.CONSTANT:
    do_something()

If the import wasn't cached, then mod.CONSTANT could be different in foo and in bar which would be very surprising.

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

2 Comments

Thank you. I was concerned about it being garbage collected because there was no explicit reference. When will bar's global namespace be deleted? During the python shutdown process?
@E.Beach -- Ahh, I understand your question now (and the reason for the function local imports). I've edited

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.