Can __iter__ be used as a classmethod?

Michele Simionato mis6 at pitt.edu
Thu Mar 6 15:27:15 EST 2003


Alex Martelli <aleax at aleax.it> wrote in message news:<FYF9a.16465$zo2.505483 at news2.tin.it>...
> Michele Simionato wrote:
>    ... I wonder if there is a direct way to extract f
> > from the classmethod ?
> 
> Sure, c.im_func is f.  That's how methods work.
> 
> 

I mean, how can I extract the function if I don't have the class?

If I convert a function

>>> def f(x): return x

to a staticmethod

>>> s=staticmethod(f)

I can have back the function via

>>> s.__get__(None)

If I have a classmethod

>>> c=classmethod(f)

I cannot do that. It seems I must define a dummy class

>>> class C(object): pass

bound the classmethod to it

>>> C.c=c

and then retrieve the original function via im_func

>>> C.c.im_func

It there must be a better way!

                                          Michele




More information about the Python-list mailing list