Reputation: 611
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"?
Upvotes: 2
Views: 449
Reputation: 3285
You could do it with something like the below:
def f(n):
exec(f"def f{n}(x): print(x)")
return eval(f"f{n}")
Upvotes: 0
Reputation: 362557
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.
Upvotes: 2