'Python 3.7 vs 2.7, assignment
I'm a bit of a 2.7 Python person. I came across this code, which I can assume what it does, but what does it exactly mean? Model is used, but I assume that model is a variable that is of the type of class, but then we assign a function to it. Looks very odd to me.
model: Class = a_function(f)
What does the above mean?
Solution 1:[1]
It’s a type annotation. It doesn’t actually do anything in code, but the annotation can be read by third-party applications (e.g. mypy) to perform type checking. In your case, the code declares that the type of the variable model is Class.
The rest of the expression is a regular assignment.
Solution 2:[2]
The following statement that you posted:
model: Class = a_function(f)
Means a variable declared with its type. This rule is not required in Python, but it's a way to show what is the type of the parameter or variable that is being used. This property is called a "type_annotation". It can be useful, for example, in complex functions implementations, such as the following code:
def process_some_data(client: str, properties: dict, feature: SomeClass):
# some code
return something
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Konrad Rudolph |
| Solution 2 | martineau |
