簡體   English   中英

functools.partial和generator

[英]functools.partial and generators

我正在嘗試執行以下操作:

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

那有什么不對?

functools.partial創建一個對象 ,其行為類似於一個模擬舊函數的新函數,其中一些參數為“凍結”。 所以你必須實際調用這個新函數來獲得相同的輸出:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM