Can __iter__ be used as a classmethod?

Michele Simionato mis6 at pitt.edu
Tue Mar 18 17:34:01 EST 2003


Alex Martelli <aleax at aleax.it> wrote in message news:<M1jca.81128$zo2.2071447 at news2.tin.it>...
> Phillip J. Eby wrote:
>    ...
> > I think it would be better to say that staticmethod is the one that
> > doesn't give any functionality.  I also think staticmethod is poorly
> > named, since it does not produce a method, and it definitely doesn't
> > do what a 'static method' does in Java or any other language I know
> > of.
> 
> Nolo contendere regarding the functionality, but I'm totally puzzled
> regarding your objection to the name.  Where's the "definitely doesn't
> do" -- isn't a Python staticmethod just like e.g. a C++ static method?
> 
> class cpp
> {
>     static void greet() {
>         std::cout << "Hello, world!\n";
>     }
> }
> 
> vs
> 
> class py:
>     def greet(): print "Hello, world!"
>     greet = staticmethod(greet)
> 
> 
> What "definite" difference is totally escaping my notice...?
> 
> 
> Alex

I think he is referring to this:

>>> class C:
...     m=lambda self,x: x 
...     s=staticmethod(lambda x:x) 
...
>>> C.m
<unbound method C.<lambda>>
>>> C.s
<function <lambda> at 0x402c2d14>
>>> C.__dict__['m']
<function <lambda> at 0x402c272c>
>>> C.__dict__['s']
<staticmethod object at 0x402f1060>

A staticmethod is not a method, is a function, when called as in the example.
On the other hand, if you are doing some metaprogramming such as wrapping 
the methods in a class by looking at C.__dict__, you must explicitly 
distinguish static methods (which are descriptors objects in this context) 
from regular methods(which now are functions). No difference at all in 
the normal usage, but big difference in metaprogramming applications.


                              Michele




More information about the Python-list mailing list