gpt4 book ai didi

Python更改方法名称

转载 作者:行者123 更新时间:2023-11-28 22:24:57 25 4
gpt4 key购买 nike

<分区>

使用 python 3.6。我正在尝试编写一些代码,允许我在类方法上放置一个唯一标识符。

这是一个例子:

def test(func):
def wrap():
pass
wrap.__name__ += '_TEST'
return wrap

class example:
@test
def method(self):pass

控制台:

import inspect
Bar = example()
inspect.getmembers(Bar, predicate=inspect.ismethod)
Out[3]:
[('method', # Name didn't change
<bound method test.<locals>.wrap of <__main__.example object at
0x000002670E073EF0>>)]
Bar.method.__name__
Out[4]: 'wrap_TEST'

我希望能够遍历一个类的方法并且只执行我用我的装饰器标记的方法。这能做到吗?

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