[Tutor] Class vs. Static Methods
Chuck Allison
chuck at freshsources.com
Thu Jun 23 01:35:34 CEST 2005
This is a neat trick. But can't this also be done with a static method
that accesses a static data attribute the same way?
Alan G wrote:
>>class Shape(object):
>> _count = 0
>>
>> @classmethod
>> def count(cls):
>> try:
>> cls._count += 1
>> except AttributeError:
>> cls._count = 1
>>
>>
>
>Ah, clever. This is where I thought I'd need an if/elif
>chain, adding a new clause for each subclass. i never thought of
>using a try/except to add an attribute to subclasses based on cls.
>
>I like it.
>
>Thanks again Kent.
>
>Alan G.
>
>_______________________________________________
>Tutor maillist - Tutor at python.org
>http://mail.python.org/mailman/listinfo/tutor
>
>
>
>
More information about the Tutor
mailing list