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.
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 .
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.
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.
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.
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]()
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)
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),
}
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,)]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With