Can __iter__ be used as a classmethod?
Alex Martelli
aleax at aleax.it
Thu Mar 6 17:35:38 EST 2003
Michele Simionato wrote:
> 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?
Who cares about the class? Just pass ANY type...
> 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
You can use almost any argument at your choice:
>>> def f(x): return x
...
>>> f
<function f at 0x4043725c>
>>> c = classmethod(f)
>>> c.__get__(int).im_func
<function f at 0x4043725c>
>>> c.__get__(c).im_func
<function f at 0x4043725c>
>>> c.__get__(23).im_func
<function f at 0x4043725c>
>>> c.__get__('as you like it').im_func
<function f at 0x4043725c>
>>>
You can't use None specifically, but thee is no need
for you to create a dummy class for the purpose, either.
Alex
More information about the Python-list
mailing list