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 ...
随机推荐
- Tomcat各个版本的下载地址包括源码
Tomcat各个版本的下载地址包括源码: http://archive.apache.org/dist/tomcat **************** 选择版本 **************** ** ...
- Notepad++正则表达式合并多行代码为1行
有时候你想要缩减代码行数,多行并1行,像网页流行的JS代码,查看时多见这种情况,但是有时你想把多行switch case缩成1行,再手动分开,每个case一行. 这种对齐方式似乎在日式程序员的代码中经 ...
- phpstudy开启PHPSocket扩展(windows系统)
PHP开启Socket扩展 一.windows系统(本地电脑) 1.打开phpstudy,设置——>配置文件——>打开php.ini(我安装的是PhpStudy v8.0,其他版本请自己找 ...
- RTSP取流设备密码含@
一.rtsp取流格式简介 RTSP的基本取流格式为:rtsp://username:password@ip_addr/... 如海康的ip地址为:rtsp://admin:admin123@10.1. ...
- Java-多线程第三篇3种创建的线程方式、线程的生命周期、线程控制、线程同步、线程通信
1.Java使用Thread类代表线程. 所有的线程对象必须是Thread类或其子类的实例. 当线程继承Thread类时,直接使用this即可获取当前线程,Thread对象的getName() ...
- Codeforces 1105C (DP)
题面 传送门 分析 这种计数问题,要不是纯数学推公式,要不就是dp 先处理出[l,r]中除3余0,1,2的数的个数,记为cnt0,cnt1,cnt2 设\(dp[i][j]\)表示前i个数的和除3余j ...
- Ecshop 商品详情页如何添加立即购买按钮
1,加到位置 <li class="add_cart_li"> <a href="javascript:addToCart1({$goods.goods ...
- linux php 中session 多站点共享session问题
linux php 中session默认file 假如修改为redis php.ini session.save_handler = "files"; session.save_p ...
- IIS故障 应用程序池“XXX”提供服务的进程在与 Windows Process Activation Service 通信时出现严重错误。该进程 ID 为“XXXX”。数据字段包含错误号。
(尝试失败,但觉得有可行性) 参考https://www.cnblogs.com/qidian10/p/6028784.html https://yq.aliyun.com/articles/6434 ...
- docker ssh连接不上
docker ssh连接报下面的错 Last login: Thu Apr 13 09:17:23 2017 from localhost Connection to 127.0.0.1 closed ...