程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长

+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

暂无数据

python高级进阶_6_无参数,有参数, 可变参数 装饰器的应用03

发布于2019-08-15 11:47     阅读(729)     评论(0)     点赞(2)     收藏(1)


版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_34979346/article/details/91354091

请看下 无参数的装饰器

如下:

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())

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

请看有两个参数的

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))  # 传入两个参数 

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

抛错如下:

 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
  • 1
  • 2
  • 3

理解下 为什么会抛错

  1. @ test 意思就是 f1=test(f1) , f1 指向是 test_in 函数体
  2. 在执行 f1(2,3) 的时候 要执行 test_in 函数体, 但是test_in 没有地方接受参数, 所以要报错
    下边我们添加上 继续执行看看结果如何:
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))


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

报错如下:

 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'
  • 1
  • 2
  • 3
  • 4
  • 5

解释如下:

  1. 在执行test_in 函数体的时候, 发现有个 func(), 它的指向 就是 f1(a,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))

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

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))



  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21


所属网站分类: 技术文章 > 博客

作者:787sds

链接:https://www.pythonheidong.com/blog/article/35914/839d3978cc150e14461b/

来源:python黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

2 0
收藏该文
已收藏

评论内容:(最多支持255个字符)