Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Misleading(?) TypeError when passing keyword arguments to function defined with positional arguments

In cPython 2.4:

def f(a,b,c,d):
    pass

>>> f(b=1,c=1,d=1)
TypeError: f() takes exactly 4 non-keyword arguments (0 given)

but:

>>> f(a=1,b=1,c=1)
TypeError: f() takes exactly 4 non-keyword arguments (3 given)

Clearly, I don't really really understand Python's function-argument processing mechanism. Anyone care to share some light on this? I see what's happening (something like filling argument slots, then giving up), but I think this would foul up a newbie.

(also, if people have better question keywords -- something like "guts" -- please retag)

like image 763
Gregg Lind Avatar asked Feb 01 '10 20:02

Gregg Lind


People also ask

How do you fix missing positional arguments in Python?

The Python "TypeError: missing 2 required positional arguments" occurs when we forget to provide 2 required arguments when calling a function or method. To solve the error, specify the arguments when calling the function or set default values for the arguments.

How do you pass a positional argument in Python?

Functions can accept a variable number of positional arguments by using *args in the def statement. You can use the items from a sequence as the positional arguments for a function with the * operator. Using the * operator with a generator may cause your program to run out of memory and crash.

What is a positional argument error in Python?

The Python rule says positional arguments must appear first, followed by the keyword arguments if we are using it together to call the method. The SyntaxError: positional argument follows keyword argument means we have failed to follow the rules of Python while writing a code.

How do you fix a missing one required positional argument?

The Python "TypeError: __init__() missing 1 required positional argument" occurs when we forget to provide a required argument when instantiating a class. To solve the error, specify the argument when instantiating the class or set a default value for the argument.


1 Answers

When you say

def f(a,b,c,d):

you are telling python that f takes 4 positional arguments. Every time you call f it must be given exactly 4 arguments, and the first value will be assigned to a, the second to b, etc.

You are allowed to call f with something like

f(1,2,3,4) or f(a=1,b=2,c=3,d=4), or even f(c=3,b=2,a=1,d=4)

but in all cases, exactly 4 arguments must be supplied.

f(b=1,c=1,d=1) returns an error because no value has been supplied for a. (0 given) f(a=1,b=1,c=1) returns an error because no value has been supplied for d. (3 given)

The number of args given indicates how far python got before realizing there is an error.

By the way, if you say

def f(a=1,b=2,c=3,d=4):

then your are telling python that f takes 4 optional arguments. If a certain arg is not given, then its default value is automatically supplied for you. Then you could get away with calling

f(a=1,b=1,c=1) or f(b=1,c=1,d=1)

like image 53
unutbu Avatar answered Oct 23 '22 20:10

unutbu