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的更多相关文章

  1. 关于arguments.callee.caller.arguments[0]获得event的一些问题

    先从一个简单的例子说起,一个简单的button控件如下: < input  type ='button'  name ='mybtn'  id ='mybtn'  onclick ='myFun ...

  2. 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 ' ...

  3. 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: [] ...

  4. Eclipse中Program arguments和VM arguments的说明

    在运行程序的时候,我们一般可以进行run configuration的配置,就比如tomcat源码导入eclipse之后,我们可以发现其运行配置如下: 其中Program arguments配置的元素 ...

  5. Run Configurations(Debug Configurations)->Arguments里填写program arguments和VM arguments

    如图: 1.program arguments存储在String[] args里 2.VM arguments设置的是虚拟机的属性,是传给java虚拟机的.KV形式存储的,是可以通过System.ge ...

  6. Eclipse 中 program arguments 与 VM arguments 的区别

    1. program arguments 中的值作为 主函数中的参数args[] 传入 2. VM Arguments 是设置的java虚拟机的属性,这些系统属性都以-D开头, VM argument ...

  7. python3 FastDFS 配置文件 客户端连接 删除文件 bug

    文件传输使用FastDFS 很方便, 不管是大小文件, 用默认的配置就可以, 这里插入一个配置文件 :  (后补python连接FastDFS上传下载文件) # connect timeout in ...

  8. arguments 对象的老历史

    引题:为什么 JavaScript 中的 arguments 对象不是数组 http://www.zhihu.com/question/50803453 JavaScript 1.0 1995 年, ...

  9. 你不知道的JavaScript--Item11 arguments对象

    1.什么是arguments arguments 是是JavaScript里的一个内置对象,它很古怪,也经常被人所忽视,但实际上是很重要的.所有主要的js函数库都利用了arguments对象.所以ag ...

随机推荐

  1. Bitter.Core系列二:Bitter ORM NETCORE ORM 全网最粗暴简单易用高性能的 NETCore ORM 之数据库连接

    Bitter.Core NETCore 相当的简单易用,下面附上使用示例: 数据中连接:请在你的NETCORE 项目中 创建:Bitter.json 配置文件,然后追加如下配置内容: MSSQL 连接 ...

  2. Linux下unix socket 读写 抓包

    Linux下unix socket 读写 抓包-ubuntuer-ChinaUnix博客 http://blog.chinaunix.net/uid-9950859-id-247877.html

  3. Android webview 问题记录

    1. Android 7.1真机安装调试apk时报错,解析安装包失败 原因:sdk版本不支持7.1版本,需要调整minSdkVersion等参数,支持低版本 解决方法: android { compi ...

  4. Nginx配置WebSocket反向代理(Tomcat+Nginx)

    @toc WebSocket 和HTTP协议不同,但是WebSocket中的握手和HTTP中的握手兼容,它使用HTTP中的Upgrade协议头将连接从HTTP升级到WebSocket.这使得WebSo ...

  5. 删除Kafka中的topic

    删除Kafka中的topic 一.配置delete.topic.enable=true 二.其他方法 一.配置delete.topic.enable=true 修改kafaka配置文件server.p ...

  6. SparkSQL访问Hive源,MySQL源

    SparkSQL访问Hive源,MySQL源 一.SparkSQL访问Hive源 软件环境 SparkSQL命令行模式可以直接连接Hive的 Java程序SparkSQL连接Hive 二.SparkS ...

  7. BGP( Border Gateway Protocol)---边界网关协议

    摘自: https://blog.csdn.net/weixin_43751619/article/details/84954755 一,BGP协议原理与配置 边界网关协议( Border Gatew ...

  8. python实现经典排序算法

    以下排序算法最终结果都默认为升序排列,实现简单,没有考虑特殊情况,实现仅表达了算法的基本思想. 冒泡排序 内层循环中相邻的元素被依次比较,内层循环第一次结束后会将最大的元素移到序列最右边,第二次结束后 ...

  9. CCPC2019网络赛总结

    比赛那会过了两道题,哇贼激动,然后后面就没有然后了... 1003我想到用$kmp$,于是不会$kmp$的我开始找板子套,结果$TLE$,就觉得应该是优化不行,优化后AC妥妥$TLE$,就放弃了. 钓 ...

  10. Codeforces Round #570 (Div. 3) B. Equalize Prices、C. Computer Game、D. Candy Box (easy version)、E. Subsequences (easy version)

    B题题意: 给你n个物品的价格,你需要找出来一个值b,使得每一个物品与这个b的差值的绝对值小于k.找到最大的b输出,如果找不到,那就输出-1 题解: 很简单嘛,找到上下限直接二分.下限就是所有物品中最 ...