Functools.partial and generators

I am trying to 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 is wrong there?

+4
source share
1 answer

functools.partialcreates an object that behaves like a new function that mimics an old function with some frozen arguments. Therefore, you should call this new function to get the same output:

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

All Articles