Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

**args in function raises TypeError "takes 0 positional arguments" [python]

Tags:

python

def f(**args):
    print(args)

This gives TypeError exception. Though when I pass no args, like f(). It just prints nothing.

What could be the reason and how to pass args? I tried with f( [4,5]), but still same result

like image 694
ERJAN Avatar asked Jun 30 '26 10:06

ERJAN


2 Answers

f only has a dictionary argument **args (more frequently written as **kwargs), which means that only keyword arguments are allowed (passing nothing at all is of course also allowed).

To enable positional arguments, add *args:

def f(*args,**kwargs)

or pass your parameter with any keyword:

f(foo=[4,5])

and kwargs["foo"] is [4,5] in f

like image 199
Jean-François Fabre Avatar answered Jul 02 '26 00:07

Jean-François Fabre


** handles keyword arguments. So your function indeed doesn't take any positional arguments, only keyword arguments (or nothing).

Positional arguments are aggregated with *args, conventionally keyword arguments with **kwargs.

like image 27
deceze Avatar answered Jul 02 '26 00:07

deceze



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!