gpt4 book ai didi

python - 使用来自不同模块的 python 装饰器函数

转载 作者:太空狗 更新时间:2023-10-29 22:20:29 24 4
gpt4 key购买 nike

我想使用来自另一个模块的函数作为装饰器,但我需要它来操作当前模块的全局命名空间。

例如,我希望能够从这里开始:

class SomeClass:
pass

root = SomeClass

为此:

from othermodule import decorator

@decorator
class Someclass:
pass

有什么想法吗?

最佳答案

这已经有效了:

from othermodule import decorator

@decorator
class Someclass:
pass

只需放入othermodule.py:

def decorator(cls):
#.... do something with cls
return cls

关于python - 使用来自不同模块的 python 装饰器函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2757496/

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