Question

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?

Was it helpful?

Solution

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top