Last active
December 11, 2015 19:49
-
-
Save mtomwing/4651348 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from collections import defaultdict | |
class Dog(object): | |
__instance_count = defaultdict(int) | |
def __init__(self): | |
self.__instance_count[self.__class__.__name__] += 1 | |
def __del__(self): | |
self.__instance_count[self.__class__.__name__] -= 1 | |
@classmethod | |
def count(cls): | |
return cls.__instance_count[cls.__name__] | |
class Puppy(Dog): | |
pass | |
if __name__ == '__main__': | |
dog = Dog() | |
pup = Puppy() | |
print 'Dog:', dog.count() | |
print 'Pup:', pup.count() | |
del pup | |
print 'Dog:', Dog.count() | |
print 'Pup:', Puppy.count() |
Author
mtomwing
commented
Jan 27, 2013
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment