python——装饰器(不定长参数,闭包,装饰器)示例
def func(functionName):
print("正在装饰")
def func_in(*args, **kargs):
print("------func_in---1--")
res = functionName(*args, **kargs)
print("------func_in---2---")
return res
print("装饰结束")
return func_in # 再还没有执行test函数的时候就开始装饰
@func
def test(a, b):
print("--------test a=%d b=%d -----" % (a, b)) @func
def test2(a, b, c, d):
print("--------test2 %d %d %d %d -------" % (a, b, c, d)) @func
def test3():
return "hahaha" print("%%%%%%%%%%%%%%%%%%% 不定长参数 %%%%%%%%%%%%%%%%")
test(1, 2)
test2(1, 2, 3, 4)
print("%%%%%%%%%%%%%%%%%%% 返回值 %%%%%%%%%%%%%%%%")
res = test3()
print("test3 return value is %s" % res)
运行结果:
正在装饰
装饰结束
正在装饰
装饰结束
正在装饰
装饰结束
%%%%%%%%%%%%%%%%%%% 不定长参数 %%%%%%%%%%%%%%%%
------func_in---1--
--------test a=1 b=2 -----
------func_in---2---
------func_in---1--
--------test2 1 2 3 4 -------
------func_in---2---
%%%%%%%%%%%%%%%%%%% 返回值 %%%%%%%%%%%%%%%%
------func_in---1--
------func_in---2---
test3 return value is hahaha
装饰器中return
from time import ctime, sleep
def func_args(args = "hello"):
def func(functionName):
print("正在装饰")
def func_in():
print("###### %s called in %s used %s ######" % (functionName.__name__, ctime(), args))
functionName()
return func_in
return func # 先执行func_args("hahah"),给args赋值,返回func的引用
# @func
# 使用@func对函数进行装饰
@func_args("hahah")
def test1():
print("--------test1--------") @func_args("heihei")
def test2():
print("---------test2----------") # 带有参数的装饰器,能够起到,在运行时,根据参数值得不同,实现不同的功能
test1()
test2()
运行结果:
正在装饰
正在装饰
###### test1 called in Sun Aug 26 14:53:34 2018 used hahah ######
--------test1--------
###### test2 called in Sun Aug 26 14:53:34 2018 used heihei ######
---------test2----------
python——装饰器(不定长参数,闭包,装饰器)示例的更多相关文章
- python函数中的参数(关键字参数,默认参数,位置参数,不定长参数)
默认参数:定义函数的时候给定变量一个默认值. def num(age=1): 位置参数:调用函数的时候根据定义函数时的形参位置和实参位置进行引用. 关键字参数:如果定义的函数中含有关键字参数,调用函数 ...
- Python 不定长参数、全局变量、局部变量 day4
一.不定长参数 在函数定义中,经常会碰到*args 和**kwargs作为参数. 事实上在函数中,*和**才是必要的,args和kwargs可以用其他名称代替 *args 是指不定数量的非键值对参数. ...
- 详解Python函数参数定义及传参(必备参数、关键字参数、默认可省略参数、可变不定长参数、*args、**kwargs)
详解Python函数参数定义及传参(必备参数.关键字参数.默认可省略参数.可变不定长参数.*args.**kwargs) Python函数参数传参的种类 Python中函数参数定义及调用函数时传参 ...
- python函数中的不定长参数
python自定义函数中有两中不定长参数,第一种是*name,第二种是**name.加了星号 * 的参数会以元组(tuple)的形式导入,存放所有未命名的变量参数.加了两个星号 ** 的参数会以字典的 ...
- python不定长参数 *argc,**kargcs(19)
在 python函数的声明和调用 中我们简单的了解了函数的相关使用,然而在函数传递参数的时候,我们埋下了一个坑,关于不定长参数的传递我们还没有讲,今天这篇文章主要就是讲解这个问题. 一.函数不定长参数 ...
- python笔记(2)---不定长参数
python自定义函数中有两种不定长参数, 第一种是*name:加了星号 * 的参数会以元组(tuple)的形式导入 第二种是**name:加了星号 * *的参数会以字典(dict)的形式导入 *na ...
- python中函数的不定长参数
例1: #定义一个含有不定长参数的函数,本例第三个参数*args def sum_nums(a,b,*args): print('_'*30) print(a) print(b) print(args ...
- python 不定长参数
1 #不定长参数 * 元祖 ** 字典 2 def item(a,b,*c,**d): 3 print(a) 4 print(b) 5 print(c) 6 print(d) 7 8 item(11, ...
- java 不定长参数
一,不定长参数的规定 一个方法只能有一个不定长参数,并且这个不定长参数必须是该方法的最后一个参数. 示例: public class VariArgs { public static void mai ...
随机推荐
- C++ STL 之 容器的深拷贝和浅拷贝
如果我们没有提供拷贝构造函数,没有重载=操作符,vector 对我们的 mc 对象进行的简单的浅拷贝,将拷贝的对象插入到容器中,导致我们的 mc 对象的 data 指针和容器中mc 对象的拷贝对象中的 ...
- Delphi 方法的声明
- Maven 安装依赖包
Guide to installing 3rd party JARs Although rarely, but sometimes you will have 3rd party JARs that ...
- idou老师教你学istio2:监控能力介绍
我们知道每个pod内都会有一个Envoy容器,其具备对流入和流出pod的流量进行管理,认证,控制的能力.Mixer则主要负责访问控制和遥测信息收集. 如拓扑图所示,当某个服务被请求时,首先会请求ist ...
- FFmpeg学习笔记之安装
本随笔原文出自:一叶知秋0830链接:https://www.jianshu.com/p/ab469a2ffd28 1.下载FFmpeg 先进入要存放下载文件的目录,比如要放在/Users/qinji ...
- mongodb命令---花样查询语句
闲言少叙 查出价格低于200的商品信息----包含商品名称,货物编号,价格,添加信息等 db.goods.find( {}}, {,,,} ) 商品分类不为3的商品 db.goods.find( {} ...
- C# Transaction 事务处理 -依赖事务
在DependentTransaction()方法中,实例化CommittableTransaction类,创建一个根事务,显示事务的信息.接着, tx.DependentClone()方法创建一个依 ...
- 转化对象为map方法
言语不多,直接上代码. private Map<String, Object> introspect(Object obj) throws Exception { Map<Strin ...
- CentOS 7 yum update 升级提示:PackageKit 锁定解决方案
CentOS 7 系列新安装后都会进行yum update操作,但每次都会遇到PackageKit 锁定问题,提示如下: /var/run/yum.pid 已被锁定,PID 为 2694 的另一个程序 ...
- bat文件设置环境变量脚本
:: 获取管理员权限 @echo off % mshta vbscript:CreateObject()(window.close)&&exit cd /d "%~dp0&q ...