python 装饰器 传递参数简单案例
def debug(func):
def wrapper(*args, **kwargs): # 指定宇宙无敌参数
print "[DEBUG]: enter {}()".format(func.__name__)
print 'Prepare and say...',
return func(*args, **kwargs)
return wrapper # 返回
@debug
def say(something):
print "hello {}!".format(something)
Python提供了可变参数*args
和关键字参数**kwargs
,有了这两个参数,装饰器就可以用于任意目标函数了。
参考:https://www.cnblogs.com/cicaday/p/python-decorator.html
我自己的例子
#!/usr/bin/env python
#todo use decorator to decorate the function that need debug and its function name
def debug(f):
def wrapper(*args,**kwargs):
print("this is the name of function: {0}".format(f.__name__))
if kwargs['username'] != 'admin':
raise Exception('you need to be admin')
f(*args,**kwargs) #装饰器内部函数的参数等于被修饰函数的参数
return wrapper
@debug
def say_hi(sth,username):
print("this is position args {0}".format(sth))
print("i am the master: {0}".format(username))
if __name__ == '__main__':
say_hi('first args',username='admin')
say_hi('first args',username='haha')
*args -- 相当于 列表 **kwargs -- 相当于字典
python 装饰器 传递参数简单案例的更多相关文章
- python3 如何给装饰器传递参数
[引子] 之前写过一篇文章用来讲解装饰器(https://www.cnblogs.com/JiangLe/p/9309330.html) .那篇文章的定位是入门级的 所以也就没有讲过多的高级主题,决定 ...
- Python第二十六天 python装饰器
Python第二十六天 python装饰器 装饰器Python 2.4 开始提供了装饰器( decorator ),装饰器作为修改函数的一种便捷方式,为工程师编写程序提供了便利性和灵活性装饰器本质上就 ...
- Python 装饰器使用指南
装饰器是可调用的对象,其参数是另一个函数(被装饰的函数). 1 装饰器基础知识 首先看一下这段代码 def deco(fn): print "I am %s!" % fn.__na ...
- 理解 python 装饰器
变量 name = 'world' x = 3 变量是代表某个值的名字 函数 def hello(name): return 'hello' + name hello('word) hello wor ...
- Python学习:10.Python装饰器讲解(一)
情景介绍 一天,在你正在努力加班的时候,老板给交给你了一个任务,就是在这段代码里将所有函数开始输出一个‘hello’最后输出当前时间,再输出一个“end”,这段代码里包含了大量的函数,你会怎么做? d ...
- Python装饰器,Python闭包
可参考:https://www.cnblogs.com/lianyingteng/p/7743876.html suqare(5)等价于power(2)(5):cube(5)等价于power(3)(5 ...
- python装饰器方法
前几天向几位新同事介绍项目,被问起了@login_required的实现,我说这是django框架提供的装饰器方法,验证用户是否登录,只要这样用就行了,因为自己不熟,并没有做过多解释. 今天查看dja ...
- Python装饰器进阶
装饰器进阶 现在,我们已经明白了装饰器的原理.接下来,我们还有很多事情需要搞清楚.比如:装饰带参数的函数.多个装饰器同时装饰一个函数.带参数的装饰器和类装饰器. 装饰带参数函数 def foo(fun ...
- Python 装饰器(进阶篇)
装饰器是什么呢? 我们先来打一个比方,我写了一个python的插件,提供给用户使用,但是在使用的过程中我添加了一些功能,可是又不希望用户改变调用的方式,那么该怎么办呢? 这个时候就用到了装饰器.装饰器 ...
随机推荐
- 几个css3动画库
Hover.css 查看演示: http://ianlunn.github.io/Hover/ github地址: https://github.com/IanLunn/Hover Animate.c ...
- 如何在vscode里面调试js和node.js
一般大家调试都是在浏览器端调试js的,不过有些时候也想和后台一样在代码工具里面调试js或者node.js,下面介绍下怎样在vscode里面走断点. 1,用来调试js 一:在左侧扩展中搜索Debugge ...
- centos使用ngnix代理https
自己建web服务器,考虑到安全问题需要用到https. 在此使用nginx的反向代理功能实现https 腾讯云证书安装指引 ssl.conf 配置 // http请求重定向https server { ...
- IoT Gateway Based on OSGi
1. OSGi Knowleage 2. OSGi.Net on Windows 3. OSGi with JAVA 4. OSGi with Qt and C++ 5. Architecture o ...
- Codeforces Round #419 A+B
A. Karen and Morning time limit per test 2 seconds memory limit per test 512 megabytes Karen is ...
- 安卓app开发-05-Android xml布局详细介绍
安卓app开发-05-Android xml布局详细介绍 虽然说有 墨刀,墨客 这些图形化开发工具来做 Android 的界面设计,但是我们还是离不开要去学习做安卓原生app,学习 xml 布局还是必 ...
- ubuntu桌面安装常用软件&及常见问题
自己从windows转向ubuntu桌面开发,根据需求安装以下文件: ubuntu 桌面版下载:http://www.ubuntu.org.cn/download/desktop 有的公司设置静态ip ...
- Java获取中文拼音、中文首字母缩写和中文首字母
获取中文拼音(如:广东省 -->guangdongsheng) /** * 得到中文全拼 * @param src 需要转化的中文字符串 * @return */ public static S ...
- Java实例---简单的超市管理系统
代码分析 Customer.java package test; public class Customer { private String name; private int customerTy ...
- laravel controller重写
<?php namespace Boss\Http\Controllers; use Illuminate\Foundation\Bus\DispatchesJobs; use Illumina ...