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 ...
随机推荐
- JavaScript 实现排序算法
参考文章: 十大经典排序算法动画,看我就够了! 1. 冒泡排序 思路 比较所有相邻元素,如果第一个比第二个大,则交换它们 一轮下来,可以保证最后一个数是最大的 执行n-1轮,就可以完成排序 代码 Ar ...
- Redis主从、哨兵模式的搭建
壹.Redis主从分离 准备三个redis配置文件(redis.conf),分别修改为redis6380.conf.redis6381.conf.redis6382.conf 一.配置Master 1 ...
- HBase与Zookeeper的关系
HBase与Zookeeper的关系 一.HBase与Zookeeper的关系 Zookeeper Client Master RegionServer 一.HBase与Zookeeper的关系 Cl ...
- Java中把对象、对象bean、list集合、对象数组、Map和Set以及字符串转换成Json
对象转换为Json 对象bean转换为Json List集合转换为Json 对象数组转换为Json Map集合转换为Json Set集合转为Json 字符串转换为Json 把Java对常用的一些数据转 ...
- python模块----paramicko模块 (ssh远程主机并命令或传文件)
paramiko模块 paramicko模块是非标准库模块,需要pip下载 paramicko:模拟ssh登陆linux主机,也有上传下载功能.ansible自动化部署软件底层就有应用paramick ...
- httprunner(2)下载安装
环境要求 HttpRunner 是一个基于 Python 开发的测试框架,可以运行在 macOS.Linux.Windows 系统平台上.这里使用macOS系统进行演示 对于python版本要求:py ...
- Jenkins(4)docker容器内部修改jenkins容器时间
前言 用docker搭建的Jenkins环境时间显示和我们本地时间相差8个小时,需修改容器内部的系统时间 查看时间 查看系统时间 date-R 进入docker容器内部,查看容器时间 docker e ...
- inceptor es表插入成功,返回报错you should set transaction.type before any DCL statement
在finebi下用星环的连接驱动去写inceptor es表,发现插入能成功,但是返回一个报错: Caused by: java.sql.SQLException: Error to commit. ...
- AtCoder Beginner Contest 178
比赛链接:https://atcoder.jp/contests/abc178/tasks A - not 题意 给出一个整数 $0 \le x \le 1$,如果 $x$ 是 $0$ 就输出 $1$ ...
- Codeforces Round #649 (Div. 2) A. XXXXX
题目链接:https://codeforces.com/contest/1364/problem/A 题意 找出大小为 $n$ 的数组 $a$ 的最长连续子数组,其元素和不被 $x$ 整除. 题解 如 ...