1

I want to keep track via a counter, every time a class is instantiated. I was trying to do this by incrementing a counter defined as a class variable from inside the class' consructor ala:

class Cl:
   cntr = 0
   def __init__(self):
      cntr += 1

However, when I instantiate the class, I get an "UnboundLocalError: local variable 'cntr' referenced before assignement" I'm assuming this is because the constructor is treating it as a local variable. How do I reference the class variable from inside a method or constructor in Python 2.7?

6
  • Possible duplicate of Difference between Class variables and Instance variables Commented Jan 17, 2018 at 20:58
  • 2
    @Stack that doesn't increment the class variable, it creates an instance variable shadowing the class variable. Commented Jan 17, 2018 at 20:58
  • Yes, of course. cntr here is marked as local by the compiler because you assign to it, but even if you didn't, it would first check locals then globals and never check the class namespace. Commented Jan 17, 2018 at 20:59
  • Possible duplicate of Python changing class variables Commented Jan 17, 2018 at 20:59
  • 3
    It's a bad design to do this in the class itself (it's not his responsibility to track instances). Do it in a metaclass, or some other external registry. Commented Jan 17, 2018 at 21:00

2 Answers 2

5

You just have to call the variable through the class:

class Cl:
    cntr = 0
    def __init__(self):
        Cl.cntr += 1  # <---Like this


print(Cl().cntr)  # prints 1
print(Cl().cntr)  # prints 2
print(Cl().cntr)  # prints 3
Sign up to request clarification or add additional context in comments.

Comments

2
class Foo:
    n = 0
    def __init__(self):
        self._increment()

    @classmethod
    def _increment(cls):
        cls.n += 1

f1 = Foo()
f2 = Foo()

>>> f1.n
2

>>> f2.n
2

1 Comment

Although, on Python 2, make sure to use a new-style class. Not sure if classmethod will behave

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.