Python 装饰器
2017-09-16 23:37
330 查看
#装饰器目的:是在执行原函数之前或之后做点其他的事 #装饰器(前) def outer(func): def inner(): print("hello") print("hello") print("hello") return func() return inner @outer def f1(): print("F1") f1()
#装饰器(后) def outer(func): def inner(): print("hello") print("hello") print("hello") r= func() print('CCC') print('CCC') print('CCC') return r return inner @outer def f1(): print("F1") f1()
#1、执行outer函数,并且将其下面的函数名当做参数
#2、将outer的返回值重新赋值给f1=outer的返回值
#新f1函数 = inner
上面是没有参数的,来两个有参数的
#装饰器(后) def outer(func): def inner(a1,a2): print("hello") print("hello") print("hello") r= func(a1,a2) print('CCC') print('CCC') print('CCC') return r return inner @outer def f1(a1,a2): print("F1") return a1+a2 print(f1(1,2))
相关文章推荐
- Python中装饰器的一个妙用
- 12步轻松搞定python装饰器
- 循序渐进Python3(四) -- 装饰器、迭代器和生成器
- Python装饰器学习(九步入门)
- Python装饰器与面向切面编程
- 简单上手Python中装饰器的使用
- @修饰符--python中的装饰器
- python使用装饰器和线程限制函数执行时间的方法
- 转:Python装饰器与面向切面编程
- python装饰器
- 12步轻松搞定python装饰器
- python装饰器之使用情景分析
- Python 装饰器
- 详解Python装饰器由浅入深
- Python_learning(装饰器)
- Python.装饰器
- python3--装饰器高级学习版
- 【脚本语言系列】关于Python基础知识装饰器,你需要知道的事
- python中装饰器的简单用法
- python 装饰器