I have a generic function for which I would like the name to change based on the value of predefined variable. Not sure this is possible in Python:
n = 'abc'
def f{n}(x):
print(x)
f{n}(3)
In the above, the function name will end up becoming "fabc"?
It is possible using a code-generation + exec
pattern. It is also possible by instantiating a types.FunctionType
instance directly. But a much simpler solution would be to leave the generic name, and then inject an alias (another reference to the same function object) for the dynamic name into whichever namespace you want.
>>> def generic_function(x):
... print(x ** 2)
...
>>> dynamic_name = "func_abc"
>>> globals()[dynamic_name] = generic_function
>>> func_abc(3)
9
To inject in some other module namespace that would be a setattr
call:
setattr(other_mod, dynamic_name, generic_function)
You could also rewrite the function's __name__
attribute if you wanted to, but there's probably not much point.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments