发布于2019-08-15 11:47 阅读(732) 评论(0) 点赞(2) 收藏(1)
如下:
def test(func):
print("test")
def test_in():
print("testing")
return "test "+func()+" test"
return test_in
def test01(func):
print("test01")
def test_in():
print("testing_01")
return "test01 "+func()+" test01"
return test_in
@test
@test01
def f1():
print("---f1----")
return "hello_word"
print(f1())
def test(func):
print("test")
def test_in():
print("testing")
return "test "+func()+" test"
return test_in
@test
def f1(a,b):
print("---f1----")
return "hello_word"
print(f1(2,3)) # 传入两个参数
抛错如下:
File "D:/works/Unittest/test.py", line 14, in <module>
print(f1(2,3))
TypeError: test_in() takes 0 positional arguments but 2 were given
理解下 为什么会抛错
def test(func):
print("test")
def test_in(a,b):
print("testing")
return "test "+func()+" test"
return test_in
@test
def f1(a,b):
print("---f1----")
return "hello_word"
print(f1(2,3))
报错如下:
File "D:/works/Unittest/test.py", line 14, in <module>
print(f1(2,3))
File "D:/works/Unittest/test.py", line 5, in test_in
return "test "+func()+" test"
TypeError: f1() missing 2 required positional arguments: 'a' and 'b'
解释如下:
def test(func):
print("test")
def test_in(a,b):
print("testing")
return "test "+func(a,b)+" test"
return test_in
@test
def f1(a,b):
print("---f1----")
return "hello_word"
print(f1(2,3))
修改如下 就可以随便传递参数。
def test(func):
print("test")
def test_in(*args,**kargs):
print("testing")
return "test "+str(func(*args,**kargs))+" test"
return test_in
@test
def f1(a,b):
print("---f1----")
return a+b
@test
def f2(a,b,c,d):
return(a+b+c+d)
print(f1(5,9))
print(f2(1,3,4,5))
作者:787sds
链接:https://www.pythonheidong.com/blog/article/35914/839d3978cc150e14461b/
来源:python黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 python黑洞网 All Rights Reserved 版权所有,并保留所有权利。 京ICP备18063182号-1
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!