python装饰器参数那些事_接受参数的装饰器
# -*- coding: utf-8 -*-
#coding=utf-8
'''
@author: tomcat
@license: (C) Copyright 2017-2019, Personal exclusive right.
@contact: liliang07@yungengxin.com
@software: coding
@file: decorator1.py
@time: 2019/7/26 11:25
''' '''
接受参数的解释器 '''
import inspect
def log(text):
def decorator(func):
def wrapper(*args,**kwargs):
print("text={},name={}".format(text,func.__name__))
res=func(*args,**kwargs)
return res
return wrapper
return decorator @log('excuse')
def add(x,y):
return x + y
print(add(3,4)) '''
接受参数的解释器
'''
def outer_decorator(*outer_args,**outer_kwargs): def decorator(fn):
def decorated(*args,**kwargs):
decorator_args = inspect.getcallargs(outer_decorator, *outer_args, **outer_kwargs)
print("outer_args={},outer_kwargs={},decorator_args={},func_name={}".format(outer_args, outer_kwargs,decorator_args,outer_decorator.__name__))
decorated_args = inspect.getcallargs(fn, *args, **kwargs)
res=fn(*args, **kwargs)
print("arg={},keord={},decorated_args={},func_name={}".format(args, kwargs, decorated_args,fn.__name__))
return res
return decorated
return decorator @outer_decorator(1,2,"test",test1=2)
def foo(a,b,c):
return a+b+c print(foo(1,6,c=6))
def f (a ,b=1,*pos,**named):
print("say hello")
print(inspect.getcallargs(f, 1, 2, 4,3,t="test"))
text=excuse,name=add
7
outer_args=(1, 2, 'test'),outer_kwargs={'test1': 2},decorator_args={'outer_args': (1, 2, 'test'), 'outer_kwargs': {'test1': 2}},func_name=outer_decorator
arg=(1, 6),keord={'c': 6},decorated_args={'a': 1, 'b': 6, 'c': 6},func_name=foo
13
{'a': 1, 'b': 2, 'pos': (4, 3), 'named': {'t': 'test'}}
python装饰器参数那些事_接受参数的装饰器的更多相关文章
- http参数的封装(后台接受参数的场景)
场景 不管是任何web框架作为一个web的开发人员必须要搞明白control层如何接受各种参数. 下面就根据我们公司的系统架构(nutz)来进行一下场景描述.各位小伙伴也可以根据这些 场景自己去总结一 ...
- Python基础(7)_闭包函数、装饰器
一.闭包函数 闭包函数:1.函数内部定义函数,成为内部函数, 2.改内部函数包含对外部作用域,而不是对全局作用域名字的引用那么该内部函数成为闭包函数 #最简单的无参闭包函数 def func1() n ...
- python学习笔记-day4笔记 常用内置函数与装饰器
1.常用的python函数 abs 求绝对值 all 判断迭代器中所有的数据是否为真或者可迭代数据为空,返回真,否则返回假 any ...
- python基础之函数进阶之函数作为返回值/装饰器
因为装饰器需要用到返回函数的知识,所以在这里将返回函数和装饰器合并讲解. 什么是返回函数? 我们知道,一个函数中return可以返回一个或者多个值,但其实,return不仅可以返回值,还可以返回函数. ...
- Python 三程三器的那些事
装饰器 1.什么是装饰器 装饰器本质是函数,用来给其他函数添加新的功能 特点:不修改调用方式.不修改源代码 2.装饰器的作用 装饰器作用:本质是函数(装饰其他函数)就是为其他函数添加其他功能 装饰器必 ...
- 【python基础】第19回 多层,有参装饰器 递归 二分法
本章内容概要 1. 多层装饰器 2. 有参装饰器 3. 递归函数 4. 算法(二分法) 本章内容详解 1. 多层装饰器 1.1 什么是多层装饰器 多层装饰器是从下往上依次执行,需要注意的是,被装饰的函 ...
- python成长之路【第四篇】:装饰器
实现装饰器的知识储备: 示例: def f1(): print("f1") 1.函数即“变量” #上面的示例中,函数f1为变量,它指向内存地址.而f1()表示函数执行. 2.高阶函 ...
- python中对变量的作用域LEGB、闭包、装饰器基本理解
一.作用域 在Python程序中创建.改变.查找变量名时,都是在一个保存变量名的空间中进行,我们称之为命名空间,也被称之为作用域.python的作用域是静态的,在源代码中变量名被赋值的位置决定了该变量 ...
- Python——day12 nonlcoal关键字、装饰器(开放封闭原则、函数被装饰、最终写法)
一.nonlocal关键字 1.作用:将L与E(E中的名字需要提前定义)的名字统一 2.应用场景:如果想在被嵌套的函数中修改外部函数变量(名字)的值 def outer(): num=10 print ...
随机推荐
- 【ABAP系列】SAP ABAP模块-任意report作为附件以邮件形式发送
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP ABAP模块-任意rep ...
- 【ABAP系列】SAP ABAP常用函数总结第一篇
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP ABAP常用函数总结第一 ...
- ALDS1_1_3_D Areas on the Cross-Section Diagram 遇见了几个有意思的语法问题
Your task is to simulate a flood damage. For a given cross-section diagram, reports areas of flooded ...
- Mac009--Axure RP安装
Mac--Axure RP安装 一.下载Axure RP8.0 下载网址:https://www.axure.com/download (下载mac版本) Axure RP说明: Axure RP是 ...
- jmeter 添加header
接口说明文档: article.fetch(通用转码服务) 通用转码服务,获取任意 url 的正文以及 title 等基本信息,仅支持 post 方法请求. 参数 参数 类型 是否必须 示例 其它说明 ...
- CentOS7 监控网络流量
首先,以下介绍的流量监控工具安装之前均需要安装epel源, 安装epel源: [root@bogon ~]# yum -y install epel-release 安装 iftop 工具,查看各个连 ...
- [LOJ3123] CTSC2019重复
Description 给定一个⻓为 n 的字符串 s , 问有多少个⻓为 m 的字符串 t 满足: 将 t 无限重复后,可以从中截出一个⻓度为 n 且字典序比 s 小的串. m ≤ 2000 n ≤ ...
- Metasploitable2使用指南
Metasploitable2使用指南 Metasploitable2 虚拟系统是一个特别制作的ubuntu操作系统,本身设计作为安全工具测试和演示常见漏洞攻击.版本2已经可以下载,并且比上一个版本包 ...
- 工作流引擎 springmvc SSM 流程审批 Java Activiti 后台框架源码
工作流模块 1.模型管理 :web在线流程设计器.预览流程xml.导出xml.部署流程 2.流程管理 :导入导出流程资源文件.查看流程图.根据流程实例反射出流程模型.激活挂起 3.运行中 ...
- 20191101php日期练习
<?phpecho abs(-23);echo "<hr/>";echo date("t");echo "<hr/>&q ...