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?
cntrhere 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.