Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

func(*args, **kwargs, x) throwing invalid syntax

Tags:

python

Studied myself into a corner again...

def superfunction(*args, **kwargs, k):
                                 ^
SyntaxError: invalid syntax

Whats the rule Im breaking here? It seems that youre not supposed to mix 'regular' variables with * variables, but I cant find anyone to confirm or deny this. I read somewhere (and I cant find in now of course) that some types of arguments have to come first, I believe keyword arguments, which may or may not be part of my issue.

like image 643
jason Avatar asked Aug 26 '13 21:08

jason


2 Answers

Try this:

def superfunction(k, *args, **kwargs):

The **kwargs variable keyword parameter must be the last part in the function declaration. Second-to-last, the *args variable position parameter. (In Python 3.x only, you can also have keyword-only parameters between *args and **kwargs.) And in the first places, the positional parameters - that's the correct way to declare function parameters. Take a look at this post for additional details.

For the full reference, see the Function definitions section in Python 3.x or Python 2.x.

like image 110
Óscar López Avatar answered Nov 09 '22 13:11

Óscar López


Syntax should be like this:

def superfunction(k, *args, **kwargs):

First you give all the positional arguments, then non-keyword arguments, and then keyword arguments.

like image 45
Rohit Jain Avatar answered Nov 09 '22 15:11

Rohit Jain