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. 重磅:保姆级Java技术图谱发布!够学到元宵节了,赶紧收藏!

    最近因为参与社群交流的时间比较多,除了唠唠白酒的嗑之外,很大一部分时间都是看到群里问到一些关于Spring Boot和Spring Cloud应用过程中碰到的问题以及一些开发过程中的报错信息.在这些帮 ...

  2. 向HDFS中指定的文件追加内容,由用户指定内容追加到原有文件的开头或结尾。

    1 import java.io.FileInputStream; 2 import java.io.IOException; 3 import java.text.SimpleDateFormat; ...

  3. list里放map list 放list

    Map<String,Integer> hashMap = new HashMap<String, Integer>(); Map<String,Integer> ...

  4. CODEVS 2542单词__fail树

    2542 单词 2013年省队选拔赛天津市队选拔赛  时间限制: 2 s  空间限制: 256000 KB  题目等级 : 大师 Master     题目描述 Description 小张最近在忙毕 ...

  5. (十)整合 JWT 框架,解决Token跨域验证问题

    整合 JWT 框架,解决Token跨域验证问题 1.传统Session认证 1.1 认证过程 1.2 存在问题 2.JWT简介 2.1 认证流程 2.2 JWT结构说明 2.3 JWT使用方式 3.S ...

  6. python输出乘法口诀

    for i in range(1,10): for j in range(1,i+1): print (" ".join(["%d*%d=%d" %(j,i,i ...

  7. 封装各种生成唯一性ID算法的工具类

    /** * Copyright (c) 2005-2012 springside.org.cn * * Licensed under the Apache License, Version 2.0 ( ...

  8. 2. Linux常用系统工作命令

    1.echo:在终端输出字符串或变量提取后的值.echo [字符串 | $变量] 举例:[root@Centos~]# echo $SHELL /bin/bash 2.date:显示及设置系统的时间或 ...

  9. (29)Vim 5

    Vim显示行号方法详解1.编辑时显示行号 在命令模式下输入":set nu"即可显示每一行的行号 如果想要取消行 号,则再次输入":set nonu"即可.2. ...

  10. 小白搭建WNMP详细教程---PHP安装与设置

    php的安装请参考WAMP中PHP的安装教程https://www.cnblogs.com/missbye/p/12049925.html 需要注意的是,我们下载的PHP版本要下载Non Thread ...