gpt4 book ai didi

python - 我可以嵌套装饰器的最高级别是多少?

转载 作者:行者123 更新时间:2023-12-01 01:42:40 25 4
gpt4 key购买 nike

TLDR

我可以将实际的装饰器包装在多少个函数下?我所说的实际装饰器是指接受目标函数作为参数的函数。

当将参数传递给 Python 中的装饰器时,我们会执行如下操作:

def decorator_maker_with_arguments(decorator_arg1, decorator_arg2):

print("I make decorators! And I accept arguments: {0}, {1}".format(decorator_arg1, decorator_arg2))

def my_decorator(func):
# The ability to pass arguments here is a gift from closures.
# If you are not comfortable with closures, you can assume it’s ok,
# or read: https://stackoverflow.com/questions/13857/can-you-explain-closures-as-they-relate-to-python
print("I am the decorator. Somehow you passed me arguments: {0}, {1}".format(decorator_arg1, decorator_arg2))

# Don't confuse decorator arguments and function arguments!
def wrapped(function_arg1, function_arg2) :
print("I am the wrapper around the decorated function.\n"
"I can access all the variables\n"
"\t- from the decorator: {0} {1}\n"
"\t- from the function call: {2} {3}\n"
"Then I can pass them to the decorated function"
.format(decorator_arg1, decorator_arg2,
function_arg1, function_arg2))
return func(function_arg1, function_arg2)

return wrapped

return my_decorator

此代码取自this answer

请注意,实际装饰器周围有一个包装器,用于处理提供给实际装饰器的参数。

奇怪的是,您可以使用包装器来装饰目标函数,而不是像这样使用装饰器:

@decorator_maker_with_arguments("Leonard", "Sheldon")
def decorated_function_with_arguments(function_arg1, function_arg2):
print("I am the decorated function and only knows about my arguments: {0}"
" {1}".format(function_arg1, function_arg2))

decorated_function_with_arguments("Rajesh", "Howard")
#outputs:
#I make decorators! And I accept arguments: Leonard Sheldon
#I am the decorator. Somehow you passed me arguments: Leonard Sheldon
#I am the wrapper around the decorated function.
#I can access all the variables
# - from the decorator: Leonard Sheldon
# - from the function call: Rajesh Howard
#Then I can pass them to the decorated function
#I am the decorated function and only knows about my arguments: Rajesh Howard

所以我的问题是,我可以在实际的装饰器中包含多少个函数?

以下代码为例:

def level0(foo):
print("Level 0")
def level1(foo):
print("Level 1")
def level2(foo):
print("Level 2")
def dec(some_func):
print("Level 3")
def wrap():
print("Foo is " + foo)
some_func()
print("Level 3 End")
return wrap
return dec
return level2
return level1

@level0("foo")
def test():
print("From python")

调用测试打印

Level 0
Level 1
TypeError: level2() missing 1 required positional argument: 'foo'

那么深度限制只有 2 吗?还是我做错了什么?

如果需要我方提供任何其他详细信息,请告诉我。

最佳答案

def level0(foo):
print(type(foo))
print("Level 0")
def level1(foo):
print(type(foo))
print("Level 1")
def level2(foo):
print("Level 2")
def dec(some_func):
print("Level 3")
def wrap():
print("Foo is " + foo)
some_func()
print("Level 3 End")
return wrap
return dec
return level2
return level1

试试这个,你就会看到 level0 和 level1 中“foo”的区别。装饰器只是语法糖。在你的情况下 python 就可以了

test = level0("foo")(test)

但是如果您的代码更改为这样

@level0
def foo():
print("from foo")

Python 就可以

test = level0(test)

关于python - 我可以嵌套装饰器的最高级别是多少?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51706694/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com