Trouble with generator as method

Peter Cole peter_cole at comcast.net
Wed Mar 22 13:21:13 EST 2006


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()

ic = t.__class__
for key in ic.__dict__.keys():
     if inspect.isfunction(ic.__dict__[key]):
         im = new.instancemethod(ic.__dict__[key], t, ic)


print t.foo
print im

print 't.foo().next() = %s' % t.foo().next()
print 'im.next() = %s' % im.next()


 >>> ================================ RESTART ==============
 >>>
<bound method T.foo of <__main__.T instance at 0x00B7ADA0>>
<bound method T.foo of <__main__.T instance at 0x00B7ADA0>>
t.foo().next() = 1

Traceback (most recent call last):
   File "D:/python242/Test Scripts/trial.py", line 22, in -toplevel-
     print 'im.next() = %s' % im.next()
AttributeError: 'function' object has no attribute 'next'
 >>>



More information about the Python-list mailing list