Trouble with generator as method

Peter Cole peter_cole at comcast.net
Wed Mar 22 21:10:08 CET 2006


Peter Otten wrote:
> 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
>  

Right, I've got it now, thanks.



More information about the Python-list mailing list