我有一段代码,如下所示:
def range_print(n, *args):
for i in range(n):
func(i)
@range_print(n=10)
def func(i):
print(i)
我得到:
TypeError Traceback (most recent call last)
<ipython-input-4-b13e44263521> in <module>
2 for i in range(n):
3 func(i)
----> 4 @range_print(n=10)
5 def func(i):
6 print(i)
<ipython-input-4-b13e44263521> in range_print(n, *args)
1 def range_print(n, *args):
2 for i in range(n):
----> 3 func(i)
4 @range_print(n=10)
5 def func(i):
TypeError: 'NoneType' object is not callable
后来我解决了这个问题,但我不知道有没有更好的方法?我想使用装饰器使我的代码看起来更干净。
def range_print(n, *args):
def inner(func): # <----Why do I need this def to load func?
for i in range(n):
func(i)
return inner
@range_print(n=10)
def func(i):
print(i)
GCT1015
相关分类