Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing no arguments while calling function through dict

Tags:

python

I have made a Python script which calls functions based on user's input. Until now I was calling argument-less functions simply through a dict

options = { 0 : func0,
            1 : func1,
            2 : func2,
           }
options[choice]()

Now I am in a situation where I need to call a few functions with arguments. I am new to Python and I tried something like this

options = { 0 : (func0,None),
            1 : (func1,None),
            2 : (func2,foo1),
            3 : (func3,foo2),
          }
options[choice][0](options[choice][1])

I am aware why None doesn't work here, and have written it just to symbolize that the function doesn't take any arguments. What changes should I make in the code so that it works for all kinds of functions?

I tried unpacking empty dict but it doesn't work either.

like image 782
nims Avatar asked Aug 05 '12 11:08

nims


People also ask

Can we call a function without passing arguments?

Using call() to invoke a function and without specifying the first argument. In the example below, we invoke the display function without passing the first argument. If the first argument is not passed, the value of this is bound to the global object. In strict mode, the value of this will be undefined .

How does a dictionary pass an argument?

Passing Dictionary as kwargs “ kwargs ” stands for keyword arguments. It is used for passing advanced data objects like dictionaries to a function because in such functions one doesn't have a clue about the number of arguments, hence data passed is be dealt properly by adding “**” to the passing type.

Is dict () and {} the same?

The setup is simple: the two different dictionaries - with dict() and {} - are set up with the same number of elements (x-axis). For the test, each possible combination for an update is run.

How do you pass multiple dictionary parameters in Python?

Use *args to take multiple arguments. Show activity on this post. Show activity on this post. If you want to avoid combining the dictionaries, then provide them as arguments rather than keyword arguments, and set their defaults to something (such as None ), and convert None to {} and otherwise copy the dictionary.


4 Answers

None is still a value, and passing it to a function that is not expecting arguments will not work. Instead consider using partial here

from functools import partial

options = { 0: func0,
            1: func1,
            2: partial(func2, foo1),
            3: partial(func3, foo2),
          }

options[choice]()
like image 59
John La Rooy Avatar answered Oct 12 '22 10:10

John La Rooy


Use a lists of arguments:

options = { 0 : (func0, []),
            1 : (func1, []),
            2 : (func2, [foo1]),
            3 : (func3, [foo2]),
          }
options[choice][0](*options[choice][1])
# or
func, args = options[choice]
func(*args)

If you want to be able to specify named arguments as well, you can extend it like this:

options = { 0 : (func0, [], {}),
            1 : (func1, [], {param_name: value}),
            2 : (print_name, [], {name: "nims"}),
            3 : (func3, [foo2], {}),
          }
func, args, kwargs = options[choice]
func(*args, **kwargs)
like image 20
phant0m Avatar answered Oct 12 '22 09:10

phant0m


You could use lambda expressions for the functions with arguments (and leave the ones without as you have them now):

options = { 0 : func0,
            1 : func1,
            2 : lambda: func2(foo1),
            3 : lambda: func3(foo2),
          }
like image 4
Michael Mauderer Avatar answered Oct 12 '22 09:10

Michael Mauderer


try something like this :

use * in function header, it'll collect all the arguments in a tuple.

def func0(*a):
    print list(a)
def func1(*a):
    print list(a)
def func2(*a):
    print list(a)
def func3(*a):
    print list(a)
foo1=1
foo2=2
options = { 0 : (func0,None),
            1 : (func1,None),
            2 : (func2,foo1),
            3 : (func3,foo2),
          }
choice=2
options[choice][0](options[choice][1:])  #prints [(1,)]

choice=1
options[choice][0](options[choice][1:])  #prints [(None,)]
like image 1
Ashwini Chaudhary Avatar answered Oct 12 '22 11:10

Ashwini Chaudhary