Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing unknown args and kwargs to function in python

Tags:

python

I have an object that takes in a function name func, args, and kwargs, and at some point runs

func(*args, **kwargs)

The issue is, if func requires no args/kwargs, args/kwargs default to None, which leads to a TypeError. For example, if the function required no parameters, args, kwargs default to None:

def test():
    pass

args = None

kwargs = None

test(*args, **kwargs)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-4-b9618694fbf2> in <module>()
----> 1 test(*args, **kwargs)

TypeError: test() argument after ** must be a mapping, not NoneType

I'm sure there's a good way to solve this without cascading if statements checking if args/kwargs exist, each with its own function call, but I'm not sure how. The main goal is to pass a function, and its unknown parameters to an object, which will then use them in a method later.

Edited: added an example for clarity

like image 571
jiminy_crist Avatar asked Jul 26 '13 15:07

jiminy_crist


People also ask

Can you pass unnamed arguments into a function in Python?

Calling functions with arbitrary arguments Just as you can define functions that take arbitrary keyword arguments, you can also call functions with arbitrary keyword arguments. By this I mean that you can pass keyword arguments into a function based on items in a dictionary.

How do you pass an unknown number argument in Python?

With Python, we can use the *args or **kwargs syntax to capture a variable number of arguments in our functions. Using *args , we can process an indefinite number of arguments in a function's position. With **kwargs , we can retrieve an indefinite number of arguments by their name.

How do you pass arguments with Kwargs Python?

Python **kwargs In the function, we use the double asterisk ** before the parameter name to denote this type of argument. The arguments are passed as a dictionary and these arguments make a dictionary inside function with name same as the parameter excluding double asterisk ** .

How do you pass arbitrary number of arguments to a function in Python?

Well, using *args in a function is Python's way to tell that this one will: Accept an arbitrary number of arguments. Pack the received arguments in a tuple named args. Note that args is just a name and you can use anything you want instead.


2 Answers

The issue is, if func requires no args/kwargs, args/kwargs default to None, which leads to a TypeError.

This is not true:

def func1(*args, **kwargs)
    print args, kwargs

will print

() {}

In reverse,

def func2(): pass

can be perfectly called with

func2(*(), **{})

So just change your args and kwargs variables and you're fine.

like image 199
glglgl Avatar answered Nov 15 '22 00:11

glglgl


Avoid to default your args and kwargs to None.

  • args should be a tuple : args = ()
  • kwargs should be a dictionnary : kwargs = {}
like image 43
Cédric Julien Avatar answered Nov 15 '22 00:11

Cédric Julien