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 ...
随机推荐
- ES5中一些重要的拓展
1.对象的拓展 ①Object.create(obj, {age:{value:18, writable:true, configurable:true, enumerable:true}); 以指定 ...
- JavaScript中的setTimeout、setInterval和随机函数制作简易抽奖小程序
几乎所有计算机语言有都内置随机函数.当然这种随机,人们习惯称为伪随机数发生器,产生的是一个[0,1)之间的一个小数.再通过简单算术运算生成一个符合需求的整数.JS中通用公式通常为parseInt(Ma ...
- CSS精灵技术(sprite)
CSS技术的使用场景:有效的减少了服务器接收和发送请求的次数,css精灵是一种出来网页背景图像的方式,将一个页面设计到的所有零星背景图集中到一张大图中去,然后将大图应用于网页 .通过backgroun ...
- CSS设置元素的隐藏和显示
常见的三种方式 display display: none 隐藏对象 display: block 除了转换为块级元素以外,同时还有显示元素的意思 特点:隐藏之后不保留位置 visibility 值h ...
- .NET 使用事物调用存储过程
using System; using System.Collections.Generic; using System.Configuration; using System.Data; using ...
- Java秒杀实战 (四)JMeter压测
转自:https://blog.csdn.net/qq_41305266/article/details/81071278. 一.JMeter入门 下载链接 http://jmeter.apache. ...
- js页面内容只读,不可复制
// document.oncontextmenu=new Function("event.returnValue=false"); // document.onselectsta ...
- Delphi 数组特性
- C 字符串几点
1.字符串结尾必须为“\0” 2.多种处理函数在<string.h> 3.常用字符串处理函数: 1.strlen 求字符串长度(\0不算在内) 2.strcpy(a,b) 将b复制到a中 ...
- 4. Scrapy框架
Scrapy 框架 Scrapy是用纯Python实现一个为了爬取网站数据.提取结构性数据而编写的应用框架,用途非常广泛. 框架的力量,用户只需要定制开发几个模块就可以轻松的实现一个爬虫,用来抓取网页 ...