Can __iter__ be used as a classmethod?

Michele Simionato mis6 at pitt.edu
Tue Mar 4 19:23:04 CET 2003


"Samuele Pedroni" <pedronis at bluewin.ch> wrote in message news:<3e649aa3_4 at news.bluewin.ch>...
> "Alex Martelli" <aleax at aleax.it> ha scritto nel messaggio
> news:dv%8a.2507$pG1.64267 at news1.tin.it...
> >
> > In Python 2.2 and later (excepting, for backwards compatibility,
> > 'classic classes') an operation always looks up the special methods
> > of interest on the CLASS (aka type) of the object being operated on.
> >
> 
> this is true (only exception is partially the static special method
> __new__), OTOH looking at what the OP was trying to do it is seems that:
> 
> - the support  for both classmethods (ignored wrt to special methods) and
> metatypes with their methods can be confusing
> 

Is there any good reason for supporting classmethods in Python ?

Looking at Guido's essay it seems they have been introduced by mistake
(trying to implement __new__ as a classmethod). 

It seems to me I could always use a regular method instead of 
a classmethod, by accessing the class trough self.__class__ or
type(self), therefore classmethods only provide a very small amount
of syntactical sugar. 

Actually they seems to me an unessential complication to the language, 
expecially because they are easily confused with the methods in the
metaclass. 

On the other hand, I like very much static methods. 

However, I noticed that

>>> isinstance(object.__new__,staticmethod)
False

Therefore if I try to retrieve all the staticmethods from a class I will
miss __new__. That's unfortunate, even if I understand this is done in order
to avoid to write 

__new__=staticmethod(__new__)

each time I redefine __new__. In a sense, __new__ is an "implicit" static
method. 

explicit-is-better-than-implicit-and-no-special-case-is-special-enough-ly 
yours,

                            Michele




More information about the Python-list mailing list