Using python 3.6. I'm trying to write some code that will allow me to to place a unique identifier on class methods.
Here's an example:
def test(func):
def wrap():
pass
wrap.__name__ += '_TEST'
return wrap
class example:
@test
def method(self):pass
Console:
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'
I want to be able to loop over methods of a class and only execute the ones I have marked with my decorator. Can this be done?
You could use a boolean flag to mark the functions. Here is my suggestion: