*args and **kwargs

Ben Finney ben+python at benfinney.id.au
Fri Sep 2 09:44:50 EDT 2016


Smith <smith at smith.com> writes:

> I'm trying to understand the concept of * args and ** kwarg with
> python3

Welcome. Your questions are fine in this forum; but you may also want to
participate in our collaborative learning forum for Python beginners,
<URL:https://mail.python.org/mailman/listinfo/tutor>.

> But I can not understand why I returns the error message "SyntaxError:
> positional argument follows the keyword argument" when I insert
> values.

It's fairly simple (though it may not be obvious!):

> In [23]: start(data="Fish",2,3,tox="tux")
>   File "<ipython-input-23-eb9c3abb9941>", line 1
>     start(data="Fish",2,3,tox="tux")
>                      ^
> SyntaxError: positional argument follows keyword argument

Exactly.

Note that this has nothing to do with how the function is defined; in
the definition of the function, parameters are neither positional nor
keyword. You name each of them, and you define an order for them; and
neither of those makes any of them “positional” or “keyword”.

Rather, “positional argument and “keyword argument” are characteristics
of the arguments you *supply* in a particular call to the function.

You have specified four arguments, in this order:

  * A keyword argument, ‘data="Fish"’.
  * A positional argument, ‘2’.
  * A positional argument, ‘3’.
  * A keyword argument, ‘tox="tux"’.

After specifying a keyword argument, you may not then specify any
positional arguments. Hence the SyntaxError.

-- 
 \           “I know that we can never get rid of religion …. But that |
  `\   doesn’t mean I shouldn’t hate the lie of faith consistently and |
_o__)                     without apology.” —Paul Z. Myers, 2011-12-28 |
Ben Finney




More information about the Python-list mailing list