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——装饰器(不定长参数,闭包,装饰器)示例的更多相关文章

  1. python函数中的参数(关键字参数,默认参数,位置参数,不定长参数)

    默认参数:定义函数的时候给定变量一个默认值. def num(age=1): 位置参数:调用函数的时候根据定义函数时的形参位置和实参位置进行引用. 关键字参数:如果定义的函数中含有关键字参数,调用函数 ...

  2. Python 不定长参数、全局变量、局部变量 day4

    一.不定长参数 在函数定义中,经常会碰到*args 和**kwargs作为参数. 事实上在函数中,*和**才是必要的,args和kwargs可以用其他名称代替 *args 是指不定数量的非键值对参数. ...

  3. 详解Python函数参数定义及传参(必备参数、关键字参数、默认可省略参数、可变不定长参数、*args、**kwargs)

    详解Python函数参数定义及传参(必备参数.关键字参数.默认可省略参数.可变不定长参数.*args.**kwargs) Python函数参数传参的种类   Python中函数参数定义及调用函数时传参 ...

  4. python函数中的不定长参数

    python自定义函数中有两中不定长参数,第一种是*name,第二种是**name.加了星号 * 的参数会以元组(tuple)的形式导入,存放所有未命名的变量参数.加了两个星号 ** 的参数会以字典的 ...

  5. python不定长参数 *argc,**kargcs(19)

    在 python函数的声明和调用 中我们简单的了解了函数的相关使用,然而在函数传递参数的时候,我们埋下了一个坑,关于不定长参数的传递我们还没有讲,今天这篇文章主要就是讲解这个问题. 一.函数不定长参数 ...

  6. python笔记(2)---不定长参数

    python自定义函数中有两种不定长参数, 第一种是*name:加了星号 * 的参数会以元组(tuple)的形式导入 第二种是**name:加了星号 * *的参数会以字典(dict)的形式导入 *na ...

  7. python中函数的不定长参数

    例1: #定义一个含有不定长参数的函数,本例第三个参数*args def sum_nums(a,b,*args): print('_'*30) print(a) print(b) print(args ...

  8. 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, ...

  9. java 不定长参数

    一,不定长参数的规定 一个方法只能有一个不定长参数,并且这个不定长参数必须是该方法的最后一个参数. 示例: public class VariArgs { public static void mai ...

随机推荐

  1. 数据分析基础之pandas & numpy

    一.jupyter的常用快捷键 - 插入cell: a, b   a是after从后插入  a是before 从前插入 - 删除cell: dd, x 都可以 - 修改cell的模式:m, y - t ...

  2. 复选框已经有checked,但是页面没有选中效果(解决)

    原代码: $("#checked").click(function(){ $(".input[name="checked"]").attr( ...

  3. C++ STL 之 list

    #include <list> #include <iostream> using namespace std; // 打印list元素 void PrintList(list ...

  4. 注意条件表达式规范[JLS 15.25]

    /** * 猜猜输出结果是什么 */ public class appalet { public static void main(String[] args) { char x = 'x'; int ...

  5. java--springmvc

    springmvc请求图 SpringMVC内部的执行流程1.用户发起到达中央调度器DispatcherServlet2.中央调度器DispatcherServlet把请求(some.do)交给了处理 ...

  6. github(工蜂)密码过期时sourcetree重新登录

  7. bash: hexo: command not found

    问题 很久没写博客了,今天用hexo新建文章时git报错: bash: hexo: command not found 解决办法 百度之后,将D:\WorkingSoftware\GithubBlog ...

  8. mybatis的配置文件详解(二)

    一.properties 这些属性都是可外部配置且可动态替换的,既可以在典型的 Java 属性文件中配置,亦可通过 properties 元素的子元素来传递.例如 1) <?xml versio ...

  9. 【高维前缀和】8.15B. 组合数

    题目分析 没有接触过高维前缀和的话会有一点抽象

  10. 题解 [AT2134] Zigzag MST

    题面 解析 我们先考虑一下加一条边(x,y,z)会成什么亚子: (还有很多边不画了...) 然后我们把这个图单独拿出来: 我们可以发现,对于最小生成树的贡献, 它是等价于下面这张图的(因为连通性一样) ...