更准确地说
@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 装饰器可以这样写吗?我对装饰器没有经验。
最佳答案
不,不能。装饰者无法访问装饰函数的内部。也许可以通过一些肮脏的黑客来实现,但恕我直言,最好避免这样的解决方案。
从您所描述的体系结构来看,您似乎需要带有实例变量的类。像这样的东西:
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
关于Python:装饰器可以从 foo1() 访问参数并将其提供给 foo2() 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16696039/