[Tutor] OO newbie

Alan Gauld alan.gauld at freenet.co.uk
Mon Apr 18 18:58:08 CEST 2005

> class SuperDict(dict):
>    count = 0
>    def __init__(self, *args, **kw):
>       self.__class__.count = self.__class__.count+1
>       ...
>       super(C,self).__init__( *args, **kw)

>...so, is count like a static attribute in Java 

Yes, it is a class variable - something that tells you about 
the whole class rather than about a single instance of 
the class. In this case it keeps tabs on how many instances 
have been created. [ And if the __del__ decrements the count 
it tells you how many are currently active.]

> I don't really understand how you access class attributes. 
> Would it just be SuperDict.count = 10?

Thats how i would do it, I'm not sure why the OP has used 
the self.__class__ magic stuff...

To me using the class name explicitly is much clearer and simpler.
But there may be some sophisticated pythonic magic at work that 
I'm missing...

Alan G.

More information about the Tutor mailing list