Python **Kwargs Example Tutorial

Published: 11 March 2017
on channel: Megawatt Apps
1,073
17

In this tutorial we go through learning about how to use python kwargs (keyword arguments) through an RPG game example.

From Python.org:
The **kwargs syntax in a function definition indicates that the interpreter should collect all keyword arguments that do not correspond to other named parameters. However, Python does not preserved the order in which those collected keyword arguments were passed to the function. In some contexts the order matters. This PEP dictates that the collected keyword arguments be exposed in the function body as an ordered mapping.

Python's **kwargs syntax in function definitions provides a powerful means of dynamically handling keyword arguments. In some applications of the syntax (see Use Cases ), the semantics applied to the collected keyword arguments requires that order be preserved. Unsurprisingly, this is similar to how OrderedDict is related to dict.

Currently to preserved the order you have to do so manually and separately from the actual function call. This involves building an ordered mapping, whether an OrderedDict or an iterable of 2-tuples, which is then passed as a single argument to the function

As Nick noted, the current behavior of **kwargs is unintuitive in cases where one would expect order to matter. Aside from more specific cases outlined below, in general "anything else where you want to control the iteration order and set field names and values in a single call will potentially benefit."

https://www.python.org/dev/peps/pep-0...


On this page of the site you can watch the video online Python **Kwargs Example Tutorial with a duration of hours minute second in good quality, which was uploaded by the user Megawatt Apps 11 March 2017, share the link with friends and acquaintances, this video has already been watched 1,073 times on youtube and it was liked by 17 viewers. Enjoy your viewing!