Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Named Argument is Keyword?

So an optional parameter expected in the web POST request of an API I'm using is actually a reserved word in python too. So how do I name the param in my method call:

example.webrequest(x=1,y=1,z=1,from=1)

this fails with a syntax error due to 'from' being a keyword. How can I pass this in in such a way that no syntax error is encountered?

like image 562
blippy Avatar asked Nov 14 '10 19:11

blippy


People also ask

What is non keyword argument in Python?

1. Non-keyword Variable Arguments (Tuple) When a function is invoked, all formal (required and default) arguments are assigned to their corresponding local variables as given in the function declaration. The remaining non-keyword variable arguments are inserted in order into a tuple for access.

What is the difference between argument and keyword argument in Python?

Python functions can contain two types of arguments: positional arguments and keyword arguments. Positional arguments must be included in the correct order. Keyword arguments are included with a keyword and equals sign.

What is a keyword argument and a non keyword argument in Python?

In the case of Normal Arguments, only the value is passed to the function definition. The number of arguments during function call should be equal to the Parameters passed in the function definition. While in the case of Keyword Arguments, you pass the argument value along with the keyword during the function call.


1 Answers

Pass it as a dict.

func(**{'as': 'foo', 'from': 'bar'})
like image 65
Ignacio Vazquez-Abrams Avatar answered Sep 28 '22 08:09

Ignacio Vazquez-Abrams