471,073 Members | 1,359 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,073 software developers and data experts.

Trouble with generator as method

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'

Mar 22 '06 #1
2 1022
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

Mar 22 '06 #2
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.
Mar 22 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

17 posts views Thread by Andrae Muys | last post: by
8 posts views Thread by Paul Chiusano | last post: by
4 posts views Thread by Wai Yip Tung | last post: by
45 posts views Thread by Joh | last post: by
12 posts views Thread by Thomas Lotze | last post: by
2 posts views Thread by ed | last post: by
5 posts views Thread by Jerzy Karczmarczuk | last post: by
11 posts views Thread by vbgunz | last post: by
2 posts views Thread by Torsten Bronger | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.