将全局值作为参数传递给装饰器和函数
Passing a global value as argument to a decorator and the function
我是 python 的新手,正在学习装饰器。没有参数和带有参数(作为字符串)的装饰器似乎很简单。但是,如果我想传递一个全局值而不是 'srting' 作为参数,它就会失败。我有所有这些不同的文件,所以我想使用全局变量。
File1:
def decorator(arg1, arg2):
def real_decorator(function):
def wrapper(*args):
print "i am decorated"
print arg1, arg2
function(*args)
return wrapper
return real_decorator
File2:
def glob(a, b):
global arg1
arg1 = a
global arg2
arg2 = b
@decorator(arg1, arg2) # pass it as 'string' ie('arg1', 'arg2') it PASSES, pass it as i have showed, it FAILS
def print_args(*args):
print 'i am ordinary'
for arg in args:
print arg
File3:
#calling functions
glob(1, 2)
print_args(3,4,5)
======================================
Pass: (when given as string)
i am decorated
arg1 arg2
i am ordinary
3
4
5
======================================
Fail: (when given global var)
Traceback (most recent call last):
File "/User/File2.py", line 19, in <module>
@decorator(arg1, arg2)
NameError: global name 'arg1' is not defined
非常感谢您对此的任何帮助。从昨天开始我就被这个问题困住了..!!
您应该在 zero-indentation 级别声明 arg1
和 arg2
,即作为模块内的全局变量:
arg1 = None
arg2 = None
def decorator(arg1, arg2):
def real_decorator(function):
def wrapper(*args):
print("i am decorated")
print(arg1, arg2)
function(*args)
return wrapper
return real_decorator
def glob(a, b):
global arg1
arg1 = a
global arg2
arg2 = b
glob(1, 2)
#calling functions
@decorator(arg1, arg2) # pass it as 'string' ie('arg1', 'arg2') it PASSES, pass it as i have showed, it FAILS
def print_args(*args):
print('i am ordinary')
for arg in args:
print(arg)
print_args(3,4,5)
此外,确保在传递 arg1
和 arg2
作为装饰器参数之前调用 glob(1, 2)
。
我是 python 的新手,正在学习装饰器。没有参数和带有参数(作为字符串)的装饰器似乎很简单。但是,如果我想传递一个全局值而不是 'srting' 作为参数,它就会失败。我有所有这些不同的文件,所以我想使用全局变量。
File1:
def decorator(arg1, arg2):
def real_decorator(function):
def wrapper(*args):
print "i am decorated"
print arg1, arg2
function(*args)
return wrapper
return real_decorator
File2:
def glob(a, b):
global arg1
arg1 = a
global arg2
arg2 = b
@decorator(arg1, arg2) # pass it as 'string' ie('arg1', 'arg2') it PASSES, pass it as i have showed, it FAILS
def print_args(*args):
print 'i am ordinary'
for arg in args:
print arg
File3:
#calling functions
glob(1, 2)
print_args(3,4,5)
======================================
Pass: (when given as string)
i am decorated
arg1 arg2
i am ordinary
3
4
5
======================================
Fail: (when given global var)
Traceback (most recent call last):
File "/User/File2.py", line 19, in <module>
@decorator(arg1, arg2)
NameError: global name 'arg1' is not defined
非常感谢您对此的任何帮助。从昨天开始我就被这个问题困住了..!!
您应该在 zero-indentation 级别声明 arg1
和 arg2
,即作为模块内的全局变量:
arg1 = None
arg2 = None
def decorator(arg1, arg2):
def real_decorator(function):
def wrapper(*args):
print("i am decorated")
print(arg1, arg2)
function(*args)
return wrapper
return real_decorator
def glob(a, b):
global arg1
arg1 = a
global arg2
arg2 = b
glob(1, 2)
#calling functions
@decorator(arg1, arg2) # pass it as 'string' ie('arg1', 'arg2') it PASSES, pass it as i have showed, it FAILS
def print_args(*args):
print('i am ordinary')
for arg in args:
print(arg)
print_args(3,4,5)
此外,确保在传递 arg1
和 arg2
作为装饰器参数之前调用 glob(1, 2)
。