Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

generic function in python - calling a method with unknown number of arguments

i'm new to python and need some help...

I'm implementing a generic search function that accepts an argument "fringe", which can be a data structure of many types.

in the search method I have the line:

 fringe.push(item, priority)

the problem is that the push method in different data structures takes different number of arguments(some require priority and some dont). is there an ellegant way to get pass that and make the "push" method take only the number of args it requires out of the argument list sent?

Thanks!

like image 740
user1049052 Avatar asked Nov 16 '11 06:11

user1049052


People also ask

How do you define a function with an unknown number of arguments in Python?

If the number of arguments to be passed into the function is unknown, add a (*) before the argument name. Lets say you want to create a function that will sum up a list of numbers. The most intuitive way will be to create that list and pass it into a function, as shown below.

How do you define a function that takes variable number of arguments?

To call a function with a variable number of arguments, simply specify any number of arguments in the function call. An example is the printf function from the C run-time library. The function call must include one argument for each type name declared in the parameter list or the list of argument types.

What is the generic function in Python?

A generic function is composed of multiple functions implementing the same operation for different types. Which implementation should be used during a call is determined by the dispatch algorithm. When the implementation is chosen based on the type of a single argument, this is known as single dispatch.


1 Answers

The method to get different number of arguments and still being able of selecting the right one is the use of *args and **keyword_args parameters. From Mark Lutz's Learning Python book:

* and **, are designed to support functions that take any number of arguments. Both can appear in either the function definition or a function call, and they have related purposes in the two locations.

* and ** in function definition

If you define a function:

def f1(param1, *argparams, **kwparams): 
    print 'fixed_params -> ', param1
    print 'argparams  --> ', argparams
    print 'kwparams   ---->,', kwparams

you can call it this way:

f1('a', 'b', 'c', 'd', kw1='keyw1', kw2='keyw2')

Then you get:

fixed_params ->  a
argparams  -->  ('b', 'c', 'd')
kwparams   ---->, {'kw1': 'keyw1', 'kw2': 'keyw2'}

So that you can send/receive any number of parameters and keywords. One typical idiom to recover keyword args is as follows:

def f1(param1, **kwparams):
    my_kw1 = kwparams['kw1']
    ---- operate  with my_kw1 ------

In this way your function can be called with any number of params and it uses those it needs.
This type or arguments are frecuently used in some GUI code like wxPython class definition and subclassing as well as for function currying, decorators, etc

* and ** in function call

* and ** params in a function call are unpacked when taken by the function:

def func(a, b, c, d):
    print(a, b, c, d)

args = (2, 3)
kwargs = {'d': 4}
func(1, *args, **kwargs)

### returns ---> 1 2 3 4 

Great!

like image 150
joaquin Avatar answered Nov 15 '22 21:11

joaquin