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?
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.
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.
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.
Pass it as a dict.
func(**{'as': 'foo', 'from': 'bar'})
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