1

更确切地说

@string_supply #This should supply **string** from foo1 into foo2
def foo2(x):
    #Cannot use global string. string changes
    return list(itertools.compress(string, x)) # **string** needed here

def foo1(startrange, string):
    temp = None
    temp_list = map(temp, start_range_compute_list(startrange))
    ls_of = map(foo2, temp_list)
    yield ls_of

@string_supply 装饰器可以这样写吗?我对装饰师没有经验。

4

1 回答 1

2

不,它不能。装饰器无法访问装饰函数的内部。也许它可以通过一些肮脏的黑客来实现,但恕我直言,最好避免这种解决方案。

从您描述的体系结构来看 - 看起来您需要带有实例变量的类。就像是:

class Simple(object):
    def __init__(self):
        self._string = ""

    def foo2(self, x):
        #Cannot use global string. string changes
        return list(itertools.compress(self._string, x)) # **string** needed here

    def foo1(self, startrange, string):
        self._string = string
        temp = None
        temp_list = map(temp, start_range_compute_list(startrange))
        ls_of = map(foo2, temp_list)
        yield ls_of
于 2013-05-22T15:53:55.003 回答