The following approach would be relatively pythonic (for my subjective judgement of pythonic - explicit, yet concise):
class CounterExample(object):
instances_created = 0
def __init__(self):
CounterExample.instances_created += 1
def __del__(self):
""" If you want to track the current number of instances
you can add a hook in __del__. Otherwise use
__init__ and just count up.
"""
CounterExample.instances_created -= 1
If you are facing a large number of classes, which need that kind of attribute, you could also consider writing a metaclass for that.
An example of a metaclass: http://www.youtube.com/watch?v=E_kZDvwofHY#t=0h56m10s.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…