Trouble with generator as method

Peter Otten __peter__ at web.de
Wed Mar 22 13:43:34 EST 2006


Peter Cole wrote:

> I'm having difficulty understanding why this doesn't work:

> import sys, new, inspect
> 
> class T:
>      def foo(self):
>         yield 1
>         yield 2
>         yield 3
> 
> 
> t = T()

im = new.instancemethod(T.foo, t, T)

> print t.foo
> print im

# prints
# <bound method T.foo of <__main__.T instance at 0x00B7ADA0>>
# <bound method T.foo of <__main__.T instance at 0x00B7ADA0>>

>From the output you can see that both are equivalent bound methods. 
 
> print 't.foo().next() = %s' % t.foo().next()
> print 'im.next() = %s' % im.next()

Yet you call t.foo() while you don't call im.

Peter
 



More information about the Python-list mailing list