The ** operator in Python is contextual or dependent on what it is used with; when used with numbers(typically between two numbers), it serves as an exponentiation operator. However in this article we will be looking at another context which it is used. We will be looking at its use as an unpacking operator, used to unpack Python dictionaries.
Anyone who has coded in Python must have seen **kwargs. Short for keyword arguments. They are arguments passed to functions in a key = value syntax. kwargs is used when we do not know the number of keyword arguments that will be passed into our function. **kwargs is a dictionary type and is as good as passing a dictionary into a function. This dictionary contains:
Going by this logic, in this article, we will be looking at its use cases in Python building up to its use case in FastAPI with Pydantic classes.
The following points will be looked at.
Note: It is not compulsory to use kwargs, you can use any other naming convention e.g. **myArgs, **anything etc.
In this example, we will have a number of keyword arguments passed to a function as **kwargs and since **kwargs is a dictionary, we will use the dictionary method .items() on it. The .items() method returns a view object that displays a list of the dictionary's key-value tuple pairs.
1 2 3 4 5 6 7 8 9 10 11 |
|
Output
1 2 3 4 5 6 7 |
|
As we must have noticed, Python classes are callable; this means that we can call a class the same way we call a function. Calling a class creates an instance (an object) of that class.
1 2 3 4 5 6 7 |
|
Calling Tech with argument values will return the instance tech.
In classes, the ** operator unpacks the dictionary allowing each key-value pair to be passed as a named argument to the class constructor.
In the example for this section, we define a class. We define a dictionary with properties matching the class parameters. We then create an instance of the class, using the ** to unpack the dictionary.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
The above code is equivalent to:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
|
This is because:
1 |
|
Is same as:
1 2 3 4 5 |
|
Pydantic is a Python library used for data validation, it is even touted as the most widely used data validation library for Python, by using Python3's type hinting system. This Pydantic employed in FastAPI helps us define data models which in simple terms are classes.
In our classes, we can specify types for our attributes or fields e.g str, int, float, List. When data is provided, Pydantic checks to make sure it matches.
In addition to this Pydantic helps with parsing and serialization. Serialization is the process of transmiting data objects into an easily transmissible format; for instance an object or array into JSON format for its simplicity and ease of parsing.
Pydantic has a BaseModel class which classes defined inherit from. Below is an example of a Pydantic model:
1 2 3 4 5 6 7 8 |
|
Suppose we have:
1 2 3 4 5 6 7 8 |
|
In the code above, item which is the request body parameter, is an instance of the Item model. It is used to validate and serialize the incoming JSON request body to ensure it matches the structure defined in th Item model.
Pydantic models have a .dict() method which returns a dictionary with the model's data.
If we create a pydantic model instance:
1 |
|
Then we call dict() with it:
1 2 |
|
We now have a dictionary and our output will be:
1 2 3 4 |
|
Note that:
1 |
|
Is equivalent to
1 2 3 4 5 6 7 8 |
|
We will now look at some situations where using the unpacking operator is beneficial.
1 2 3 4 5 |
|
1 2 3 4 5 6 |
|
The dictionary unpacking operator ** is one to consider using because of its dynamic nature of handling arguments in functions and classes, and in merging and creation of new dictionaries. All these put together leads to lesser code and better maintenance of code.
The above is the detailed content of The Use of The ** Operator With Python and FastAPI Pydantic Classes. For more information, please follow other related articles on the PHP Chinese website!