numinstances use class method instead of static def __init__(self)spam numinstances + def printnuminstances(cls)print("number of instances%scls numinstancesprintnuminstances classmethod(printnuminstancesthis class is used in the same way as the prior versionsbut its printnuminstances method receives the spam classnot the instancewhen called from both the class and an instancefrom spam_class import spam ab spam()spam( printnuminstances(number of instances spam printnuminstances(number of instances passes class to first argument also passes class to first argument when using class methodsthoughkeep in mind that they receive the most specific ( lowestclass of the call' subject this has some subtle implications when trying to update class data through the passed-in class for exampleif in module spam_class py we subclass to customize as beforeaugment spam printnuminstances to also display its cls argumentand start new testing sessionclass spamnuminstances trace class passed in def __init__(self)spam numinstances + def printnuminstances(cls)print("number of instances% % (cls numinstancescls)printnuminstances classmethod(printnuminstancesclass sub(spam)def printnuminstances(cls)override class method print("extra stuff "clsbut call back to original spam printnuminstances(printnuminstances classmethod(printnuminstancesclass other(spam)pass inherit class method verbatim the lowest class is passed in whenever class method is runeven for subclasses that have no class methods of their ownfrom spam_class import spamsubother sub( spam( printnuminstances(extra stuff number of instances sub printnuminstances(extra stuff number of instances printnuminstances(number of instances advanced class topics call from subclass instance call from subclass itself call from superclass instance