简体   繁体   中英

functools.partial and generators

I am trying do the following:

import functools

class TestClass():
    def method(self, n):
        for i in xrange(n):
            yield i

# This works 
for x in TestClass().method(10):
    print x

# This gets a TypeError: functools.partial object not iterable
for x in functools.partial(TestClass().method, 10):
    print x

What wrong there?

functools.partial creates an object that behaves like a new function that mimics the old function with some arguments 'frozen'. So you have to actually call this new function to get the same output:

for x in functools.partial(TestClass().method, 10)():
    print x

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM