Can __iter__ be used as a classmethod?

Alex Martelli aleax at aleax.it
Tue Mar 11 09:28:05 EST 2003


Greg Ewing (using news.cis.dfn.de) wrote:

> Alex Martelli wrote:
>> Aren't these reasons enough?  How else would you code a method that
>> you can indifferently call either on any instance or on the class
>> object itself?
> 
> How often do people actually *need* that functionality,

Not often!  The same could be said of MANY other things, I
believe, including of course staticmethod.  How many of the
features introduced in Python 1.6 and later are actually 
NEEDED, considering that 1.5.2 was already quite usable to
write large and complicated applications in?

"NEED" is a very strong word, after all.

> though? Smalltalkers don't seem to be bothered by the
> need to know whether to call a method on a class or an
> instance. Can someone provide a compelling use case
> for this in Python?

If you provide any compelling use case for staticmethod's 
ability to perform the same thing, I think a parallel can 
surely be drawn that shows a similar use case for classmethod's 
slightly richer abilities with exactly the same context.


>> And _having_ to write a custom metaclass as the
>> only way to get classmethods would be somewhat of an overkill.
> 
> What if there were some syntactic support, such as
> 
>    class C:
> 
>      def class foo(cls):
>        ...
> 
> which would trigger the automatic creation of a
> suitable metaclass. Would there still be a pressing
> need for classmethod()-style class methods then?

I think that using a custom metaclass has implications that
may be considered a bit too strong to sneak one in "by black
magic" -- what happens if you're multiply inheriting from
a class C and a class D and both have class methods?  Should
we have a FURTHER custom metaclass generated then?  Magic
gets blacker and blacker...

What do you find so horrible with classmethod as it stands
that would come close to justifying such an excursion into
exhoterism in order to get rid of it?


Alex





More information about the Python-list mailing list