Python **Kwargs Example Tutorial

Publié le: 11 mars 2017
sur la chaîne: 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...


Sur cette page du site, vous pouvez voir la vidéo en ligne Python **Kwargs Example Tutorial durée heure minute seconde en bonne qualité , qui a été Téléchargé par l'utilisateur Megawatt Apps 11 mars 2017, Partagez le lien avec vos amis et connaissances, sur youtube cette vidéo a déjà été regardée 1,073 fois et il a aimé 17 téléspectateurs. Bon visionnage!