Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A way to automatically pass parameters to a function?

I have a bunch of callback functions in a module. They all call some_func() with the first, say, couple of arguments always the same and derived from their own arguments, while other arguments vary. Like so:

from outer.space import some_func
def callback_A(param1, param2, param3):
    ...
    some_func(param2+param1, param3, ..., ...)
    ...

def callback_B(param1, param2, param3, param4):
    ...
    some_func(param2+param1, param3, ..., ...)
    ...

And it's all over the code. And uglier than just param2+param1.

In C/C++, I'd just do a macro

#define    S_F(a,b)    some_func(param2+param1, param3, a, b)

and start using S_F inside callbacks instead of some_func. What can I do in Python?

like image 418
Alexander Pavlov Avatar asked Nov 14 '12 13:11

Alexander Pavlov


People also ask

How do you pass a parameter to a function in Python?

Function blocks begin with the keyword def followed by the function name and parentheses ( ( ) ). Any input parameters or arguments should be placed within these parentheses. You can also define parameters inside these parentheses.

What is it called when a parameter is passed to a function?

Call by value:- The call by value method of passing arguments to a function copies the actual value of an argument into the formal parameter of the function. By default, C programming uses call by value to pass arguments.


2 Answers

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.

like image 146
Bakuriu Avatar answered Sep 25 '22 23:09

Bakuriu


You could base it on decorators:

import functools
from outer.space import some_func

def with_some(f):
    @functools.wraps(f)
    def wrapper(param1, param2, param3, *args):
        new_args = f(*args)
        return some_func(param1+param2, param3, *new_args)

    return wrapper

@with_some
def callback_A():
    return ()  # Will call some_func(param1 + param2, param3)

...

@with_some
def callback_B(param4):
    return param4,  # some_func(param1 + param2, param3, param4)

The wrapped functions will all have signature f(param1, param2, param3, *args)

like image 20
bereal Avatar answered Sep 25 '22 23:09

bereal