# -*- 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装饰器参数那些事_接受参数的装饰器的更多相关文章

  1. http参数的封装(后台接受参数的场景)

    场景 不管是任何web框架作为一个web的开发人员必须要搞明白control层如何接受各种参数. 下面就根据我们公司的系统架构(nutz)来进行一下场景描述.各位小伙伴也可以根据这些 场景自己去总结一 ...

  2. Python基础(7)_闭包函数、装饰器

    一.闭包函数 闭包函数:1.函数内部定义函数,成为内部函数, 2.改内部函数包含对外部作用域,而不是对全局作用域名字的引用那么该内部函数成为闭包函数 #最简单的无参闭包函数 def func1() n ...

  3. python学习笔记-day4笔记 常用内置函数与装饰器

    1.常用的python函数 abs             求绝对值 all               判断迭代器中所有的数据是否为真或者可迭代数据为空,返回真,否则返回假 any          ...

  4. python基础之函数进阶之函数作为返回值/装饰器

    因为装饰器需要用到返回函数的知识,所以在这里将返回函数和装饰器合并讲解. 什么是返回函数? 我们知道,一个函数中return可以返回一个或者多个值,但其实,return不仅可以返回值,还可以返回函数. ...

  5. Python 三程三器的那些事

    装饰器 1.什么是装饰器 装饰器本质是函数,用来给其他函数添加新的功能 特点:不修改调用方式.不修改源代码 2.装饰器的作用 装饰器作用:本质是函数(装饰其他函数)就是为其他函数添加其他功能 装饰器必 ...

  6. 【python基础】第19回 多层,有参装饰器 递归 二分法

    本章内容概要 1. 多层装饰器 2. 有参装饰器 3. 递归函数 4. 算法(二分法) 本章内容详解 1. 多层装饰器 1.1 什么是多层装饰器 多层装饰器是从下往上依次执行,需要注意的是,被装饰的函 ...

  7. python成长之路【第四篇】:装饰器

    实现装饰器的知识储备: 示例: def f1(): print("f1") 1.函数即“变量” #上面的示例中,函数f1为变量,它指向内存地址.而f1()表示函数执行. 2.高阶函 ...

  8. python中对变量的作用域LEGB、闭包、装饰器基本理解

    一.作用域 在Python程序中创建.改变.查找变量名时,都是在一个保存变量名的空间中进行,我们称之为命名空间,也被称之为作用域.python的作用域是静态的,在源代码中变量名被赋值的位置决定了该变量 ...

  9. Python——day12 nonlcoal关键字、装饰器(开放封闭原则、函数被装饰、最终写法)

    一.nonlocal关键字 1.作用:将L与E(E中的名字需要提前定义)的名字统一 2.应用场景:如果想在被嵌套的函数中修改外部函数变量(名字)的值 def outer(): num=10 print ...

随机推荐

  1. 03 | 基础篇:经常说的 CPU 上下文切换是什么意思?(上)

    上一节,我给你讲了要怎么理解平均负载( Load Average),并用三个案例展示了不同场景下平均负载升高的分析方法.这其中,多个进程竞争 CPU 就是一个经常被我们忽视的问题. 我想你一定很好奇, ...

  2. C++ STL:优先队列的使用详解

    堆是一个很重要的数据结构,那么我们如何更加简洁的去写大根/小根堆呢? 对于很多语言来说,只能一步一步手打,但是对于C++来说,写大根小根堆就简便得多,因为C++中有一个容器叫做priority_que ...

  3. 继承Process类,另一种方法计算累加和以及阶乘

    #定义一个类 继承Process类 from multiprocessing import Process import os import time class jiecheng(Process): ...

  4. DS-哈希表浅析

    1.哈希表 2.哈希函数 3.哈希冲突 哈希表 哈希表是一种按key-value存储的数据结构,也称散列表. 之前的数组.树和图等等查找一个值时都要与结构中的值相比较,查找的效率取决于比较的次数. 而 ...

  5. C#=> 栈模仿堆的操作

    //原理,利用两个栈,互相作用,来模仿堆的效果,先进先出.. using System; using System.Collections.Generic; using System.Linq; us ...

  6. from、includes、indexOf

    from.includes.indexOf:https://blog.csdn.net/j59580/article/details/53897630?utm_source=blogxgwz1 语法 ...

  7. smarty中判断数组是否为空的方法

    1,用count来取得数组的下标个数 下面例子中,如果$array为空则不输出任何数据 以下为引用的内容:{if $array|@count neq 0 }... ...{/if} 2,直接来判断 以 ...

  8. redis 哨兵配置文件解读sentinel.conf

    # Example sentinel.conf # port <sentinel-port>port 8001 # 守护进程模式daemonize yes # 指明日志文件名logfile ...

  9. Codeforces - 1191B - Tokitsukaze and Mahjong - 模拟

    https://codeforces.com/contest/1191/problem/B 小心坎张听的情况. #include<bits/stdc++.h> using namespac ...

  10. vue图片预加载

    目的: 图片预加载能够使得用户在浏览后续页面的时候,不会出现图片加载一半导致浏览不流畅的情况. 一.方法一 项目打开的时候要对图片进行预加载,在App.vue里面的beforeCreate添加预加载程 ...