You can use functools.partial
>>> from functools import partial
>>> def my_function(a,b,c,d,e):
... print (a,b,c,d,e)
...
>>> func_with_defaults = partial(my_function, 1, 2, e=5)
>>> func_with_defaults(3, 4)
(1, 2, 3, 4, 5)
Edit:
Since you do not have these values in advance you can't use partial
or lambda
.
You may be tempted to do this:
>>> A = lambda x: x + y
>>> def do_something(y):
... return A(2) # hope it uses the `y` parameter...
...
>>> do_something(1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in do_something
File "<stdin>", line 1, in <lambda>
NameError: global name 'y' is not defined
But as can you see it does not work. Why? Because when you define a function python saves the scope in which you defined it and uses it to resolve the global
/nonlocal
variables.
If you had access to some_func
it would be possible to do what you want by "hacking" the interpreter stack using inspect
but this is not a robust nor elegant thing to do, so do not do that.
What I'd do in your case is to simply rewrite the statement.
If you really want to avoid this, you can try something using exec
:
>>> def some_function(a,b,c):
... print(a,b,c)
...
>>> code = 'some_function(a+b,c,%s)'
>>>
>>> def func_one(a,b, c):
... exec code % 1
...
>>> def func_two(a,b,c):
... exec code % 2
...
>>> func_one(1,2,3)
(3, 3, 1)
>>> func_two(1,2,3)
(3, 3, 2)
But this is ugly.
If you use only positional arguments to your function you may do something more elegant such as:
>>> def compute_values(a,b,c):
... return (a+b, c)
...
>>> def func_one(a,b,c):
... some_function(*(compute_values(a,b,c) + (1,)))
...
>>> def func_two(a,b,c):
... some_function(*(compute_values(a,b,c) + (2,)))
...
>>> func_one(1,2,3)
(3, 3, 1)
>>> func_two(1,2,3)
(3, 3, 2)
But at this point you are just repeating a different text, and you lost much readability.
If you want to have preprocessing features in python you may try Python Preprocessing, even though, in your case, I'd rather just repeat the function calls.