Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 898
Ruediger Jungbeck Avatar asked Jan 19 '14 19:01

Ruediger Jungbeck


1 Answers

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
like image 185
KillianDS Avatar answered Sep 30 '22 03:09

KillianDS