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 ...
随机推荐
- 使用LEANGOO泳道
转自:https://www.leangoo.com/leangoo_guide/leangoo_yongdao.html 列表使用纵向的纬度管理卡片,通常代表卡片的工作的不同阶段,或者任务的状态.泳 ...
- 如何从零搭建hexo个人博客网站
https://www.jianshu.com/p/adf65cbad393?utm_source=oschina-app 准备工作 github账号 node.js 环境搭建 git使用 mar ...
- 【Git】六、分支管理&冲突解决
上一节讲了如何和远端的仓库协同工作,这一节介绍一下分支 ---------------------------- 提要 //创建一个分支dev $ git branch dev //切换到dev分支 ...
- GDB调试器教程
启动和退出GDBGDB(GNU Project Debugger)几乎适用于所有类Unix系统,小巧方便且不失功能强大,Linux/Unix程序员经常用它来调试程序. 总的来说有几下几种方法启动GDB ...
- C#实现10进制转2进制
这几天在复习计算机原理,看到二进制忽然想到二进制转10进制的公式,然后转念一想10进制转二进制的公式好像没印象,那索性自己写出来. 结果学渣的我发现,并不能写出来!什么数列,对数,xx函数忘得一干二净 ...
- [唐胡璐]QTP技巧 - QTP菜单项消失
有时候QTP的菜单栏的下拉菜单为空。 解决方法:在菜单栏点击右键,选择“Customize”,在Customize窗口的ToolBarTab页,点击“Restore All”后即可。
- Java8-Stream-No.01
import java.util.ArrayList; import java.util.List; import java.util.Optional; public class Streams1 ...
- A Hands-on Look at Using Ray Tracing in Games with UE 4.22 GDC 2019
A Hands-on Look at Using Ray Tracing in Games with UE 4.22 GDC 2019 talker: Sjoerd De Jong (SR.ENGIN ...
- ACM-ICPC 2017 沈阳赛区现场赛 M. Wandering Robots && HDU 6229(思维+期望)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6229 参考题解:https://blog.csdn.net/lifelikes/article/det ...
- Codeforces Round #586 (Div. 1 + Div. 2) B. Multiplication Table
链接: https://codeforces.com/contest/1220/problem/B 题意: Sasha grew up and went to first grade. To cele ...