5

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?

Ruediger Jungbeck
  • 2,836
  • 5
  • 36
  • 59

1 Answers1

8

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
KillianDS
  • 16,936
  • 4
  • 61
  • 70