This is a repo where I collect some functions I found somehow useful to modify PyObjects with the Python C-API. The code is written in C++.
In [1]: importpython_hacks ...: importsysIn [2]: defa_function(): ...: return"HelloWorld!"In [3]: python_hacks.change_function_name(sys.modules[__name__], "a_function", "helloWorld")
In [4]: a_function() ---------------------------------------------------------------------------NameErrorTraceback (mostrecentcalllast)
<ipython-input-4-47817fb05fae>in<module>---->1a_function()
NameError: name'a_function'isnotdefinedIn [5]: helloWorld() Out[5]: 'HelloWorld!'In [6]: classA: ...: defa_method(self): ...: return"HelloWorld!" ...: In [7]: a=A() In [8]: a.a_method() Out[8]: 'HelloWorld!'In [9]: python_hacks.change_function_name(A, "a_method", "helloWorld")
In [10]: a.helloWorld() Out[10]: 'HelloWorld!'In [11]: a.a_method() ---------------------------------------------------------------------------AttributeErrorTraceback (mostrecentcalllast)
<ipython-input-17-2952ca682861>in<module>---->1a.a_method()
AttributeError: 'A'objecthasnoattribute'a_method'In [1]: importpython_hacksIn [2]: defa_function(): ...: return"HelloWorld!" ...: In [3]: print(a_function) <functiona_functionat0x107ee4ea0>In [4]: python_hacks.change_function_repr(a_function, lambdax: f"<function {x.__name__} at not {hex(id(x))}>") In [5]: print(a_function) <functiona_functionatnot0x107ee4ea0>