If the functions are in fact one and the same object, you can just do f == g and see whether they are the same object.
Secondly, if functions have the same bytecode ( f.func_code.co_code ), then they are equivalent.
Equivalent (but probably more portable), you can use dis.dis to get the same information. Please note that this will be subject to false negatives, as in this case.
I understand that dill will be better, and will allow you to get the function text. Using this information, you can use ast to analyze text and perform similar analyzes to optimize compilers to decide whether code can be "optimized" into the same syntax tree. Again, there will be functionally equivalent functions that cannot simply be reduced to the same ast.
So, yes, for some pairs of functionally equivalent functions this detection is possible, but there will always be false negatives.
source share