Question about accessing class-attributes.
Alex Martelli
aleax at aleax.it
Thu Apr 24 09:27:42 EDT 2003
Andrew Bennetts wrote:
> On Thu, Apr 24, 2003 at 01:29:44PM +0200, Nikolai Kirsebom wrote:
>> Given the following class hierarchy:
>>
>> class A(object):
>> InstCount = 0
>> def __init__(self):
>> self.__class__.__dict__['InstCount'] += 1
>
> A simpler way to do this line would be:
>
> self.__class__.InstCount += 1
However, this hides a bug with subclasses that fail to redefine
InstCount in their bodies and turns it into weird behavior instead.
I suggest a custom metaclass -- see my earlier post in this thread.
Alex
More information about the Python-list
mailing list