【发布时间】:2019-10-20 04:04:11
【问题描述】:
我是学习 python 的新手,我在使用装饰器时遇到了一个问题。我无法从@decorate1 打印结果。如果您能帮我解决这个问题,我将不胜感激。请在下面找到代码:
def decorate1(function):
def wrapper(*args):
print("the arguments are ",args)
return wrapper
def fileoperation(function):
def wrapper():
f=function()
with open("sample.txt",'w') as wf:
wf.write(f)
return wrapper
def listoperation(function):
def wrapper(*args):
mylst=[]
for i in args:
mylst.append(i)
print(mylst)
return wrapper
@listoperation
@decorate1
def display(name,age):
pass
display('vijay',31)
@fileoperation
def filestring():
return "This is a file"
filestring()
Output:
['vijay', 31]
[Finished in 0.2s]
【问题讨论】:
-
装饰1之前缺少@
标签: python-3.x python-2.7 python-decorators