Python3 & Decorators with arguments & @Decorators with arguments bug
Python3 & Decorators with arguments & @Decorators with arguments bug

@Decorators with arguments bug
# add support args
def decor(func, args):
def wrap(args):
print("======before calling function======")
func(args)
print("======after called function======")
return wrap
#
def greeting(name):
print("Hello world!", name)
decorated = decor(greeting, '')
decorated('xgqfrms')
# bug
@decor('')
def greeting(name):
print('@修饰符号')
print('Hello world!', name)
greeting('xgqfrms')
__init__ & __call__
# Decorators with Arguments
"""
https://python-3-patterns-idioms-test.readthedocs.io/en/latest/PythonDecorators.html#decorators-with-arguments
https://www.geeksforgeeks.org/decorators-with-parameters-in-python/
https://stackoverflow.com/questions/5929107/decorators-with-parameters
"""
# PythonDecorators/decorator_with_arguments.py
class decorator_with_arguments(object):
def __init__(self, arg1, arg2, arg3):
# TypeError: __init__() takes 4 positional arguments but 5 were given
"""
If there are decorator arguments, the function
to be decorated is not passed to the constructor!
"""
print("1. Inside __init__()")
self.arg1 = arg1
self.arg2 = arg2
self.arg3 = arg3
def __call__(self, f):
"""
If there are decorator arguments, __call__() is only called
once, as part of the decoration process! You can only give
it a single argument, which is the function object.
"""
print("2. Inside __call__()")
def wrapped_f(*args):
print("\n5. Inside wrapped_f()")
print("6. Decorator arguments:", self.arg1, self.arg2, self.arg3)
f(*args)
print("8. After f(*args)")
# return OK ??? __call__ 闭包函数 ???
return wrapped_f
# return bug
# return wrapped_f
@decorator_with_arguments("a1", "a2", "a3")
def sayHello(a1, a2, a3, a4):
print('7. sayHello arguments: \n', a1, a2, a3, a4)
# @decorator_with_arguments("a1", "a2", "a3")
# def sayHello(a1, a2, a3, a4):
# print('sayHello arguments:', a1, a2, a3, a4)
print("3. After decoration")
print("4. Preparing to call sayHello()")
sayHello("say", "hello", "argument", "list")
# TypeError: 'NoneType' object is not callable
print("9. after first sayHello() call\n")
sayHello("a", "different", "set of", "arguments")
print("9. after second sayHello() call")
"""
# test
$ python3 decorators-at-with-arguments.py
"""
"""
1. Inside __init__()
2. Inside __call__()
3. After decoration
4. Preparing to call sayHello()
5. Inside wrapped_f()
6. Decorator arguments: a1 a2 a3
7. sayHello arguments:
say hello argument list
8. After f(*args)
9. after first sayHello() call
5. Inside wrapped_f()
6. Decorator arguments: a1 a2 a3
7. sayHello arguments:
a different set of arguments
8. After f(*args)
9. after second sayHello() call
"""
refs
https://www.cnblogs.com/xgqfrms/p/13494857.html

https://www.sololearn.com/Discuss/New
xgqfrms 2012-2020
www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!
Python3 & Decorators with arguments & @Decorators with arguments bug的更多相关文章
- 关于arguments.callee.caller.arguments[0]获得event的一些问题
先从一个简单的例子说起,一个简单的button控件如下: < input type ='button' name ='mybtn' id ='mybtn' onclick ='myFun ...
- NoReverseMatch at /salesman/zhuce/ Reverse for '/zhuce/' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []
NoReverseMatch at /salesman/zhuce/ Reverse for '/zhuce/' with arguments '()' and keyword arguments ' ...
- Django Reverse for 'artic_post' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []
Reverse for 'home' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: [] ...
- Eclipse中Program arguments和VM arguments的说明
在运行程序的时候,我们一般可以进行run configuration的配置,就比如tomcat源码导入eclipse之后,我们可以发现其运行配置如下: 其中Program arguments配置的元素 ...
- Run Configurations(Debug Configurations)->Arguments里填写program arguments和VM arguments
如图: 1.program arguments存储在String[] args里 2.VM arguments设置的是虚拟机的属性,是传给java虚拟机的.KV形式存储的,是可以通过System.ge ...
- Eclipse 中 program arguments 与 VM arguments 的区别
1. program arguments 中的值作为 主函数中的参数args[] 传入 2. VM Arguments 是设置的java虚拟机的属性,这些系统属性都以-D开头, VM argument ...
- python3 FastDFS 配置文件 客户端连接 删除文件 bug
文件传输使用FastDFS 很方便, 不管是大小文件, 用默认的配置就可以, 这里插入一个配置文件 : (后补python连接FastDFS上传下载文件) # connect timeout in ...
- arguments 对象的老历史
引题:为什么 JavaScript 中的 arguments 对象不是数组 http://www.zhihu.com/question/50803453 JavaScript 1.0 1995 年, ...
- 你不知道的JavaScript--Item11 arguments对象
1.什么是arguments arguments 是是JavaScript里的一个内置对象,它很古怪,也经常被人所忽视,但实际上是很重要的.所有主要的js函数库都利用了arguments对象.所以ag ...
随机推荐
- https://stackoverflow.com/questions/3232943/update-value-of-a-nested-dictionary-of-varying-depth
https://stackoverflow.com/questions/3232943/update-value-of-a-nested-dictionary-of-varying-depth p.p ...
- Python学习【第2篇】:循环
For循环 pass while 循环 pass 练习题: 1.使用while循环输入 1 2 3 4 5 6 8 9 10,不输出7 n = 1while n< 11: if n == ...
- Flink-v1.12官方网站翻译-P026-State Backends
状态后台 Flink提供了不同的状态后端,指定状态的存储方式和位置. 状态可以位于Java的堆上或离堆.根据你的状态后端,Flink还可以为应用程序管理状态,这意味着Flink处理内存管理(必要时可能 ...
- Flink-v1.12官方网站翻译-P013-Timely Stream Processing
及时的流处理 介绍 及时流处理是有状态流处理的一种扩展,其中时间在计算中起着一定的作用.其中,当你做时间序列分析时,当做基于某些时间段(通常称为窗口)的聚合时,或者当你做事件处理时,事件发生的时间很重 ...
- 如何使用命令将文件夹中的文件名(包括路径)写入到txt文件中
在cmd中使用 cd /d 路径,进入当前文件夹中 使用 dir /s /b > 0.txt 如图:
- GPLT L2-024 部落 (并查集)
N ≤ 104,输入如下数据如果没有路径压缩可能会超时. 10000 2 1 2 2 3 4 2 5 6 -- 2 9997 9998 2 9999 10000 2 9999 9997 -- 2 5 ...
- map详细的复习
map 就是一种基于自建红黑树的 一一对应的hash 的容器 通过模板方式实现 map<type,type> mapname: 前边是key 后边是 vale 转载如下作者:sevenc ...
- Educational Codeforces Round 88 (Rated for Div. 2) B. New Theatre Square(贪心)
题目链接:https://codeforces.com/contest/1359/problem/B 题意 有一块 $n \times m$ 的地板和两种瓷砖: $1 \times 1$,每块花费为 ...
- Educational Codeforces Round 86 (Div. 2)
比赛链接:https://codeforces.com/contest/1342 A - Road To Zero 题意 有两个非负整数 x, y 以及两种操作: 支付 a 点代价使其中一个数加一或减 ...
- 【noi 2.6_2989】糖果(DP)
题意:求取到总和为K的倍数的糖果的最大值. 解法:用模K的余数作为一个维度,f[i][j]表示在前i种糖果中取到总颗数模K余j的最大总颗数. 注意--f[i-1][j]要正常转移,而其他要之前的状态存 ...