There is a ->
, or dash-greater-than symbol at the end of a python method, and I'm not sure what it means. One might call it an arrow as well.
Here is the example:
@property def get_foo(self) -> Foo: return self._foo
where self._foo
is an instance of Foo.
My guess is that it is some kind of static type declaration, to tell the interpreter that self._foo
is of type Foo. But when I tested this, if self._foo
is not an instance of Foo, nothing unusual happens. Also, if self._foo
is of a type other than Foo, let's say it was an int
, then type(SomeClass.get_foo())
returns int
. So, what's the point of -> Foo
?
This concept is hard to lookup because it is a symbol without a common name, and the term "arrow" is misleading.
This is function annotations. It can be use to attach additional information to the arguments or a return values of functions. It is a useful way to say how a function must be used.
The None keyword is used to define a null value, or no value at all. None is not the same as 0, False, or an empty string.
In Python, a function is a group of related statements that performs a specific task. Functions help break our program into smaller and modular chunks. As our program grows larger and larger, functions make it more organized and manageable. Furthermore, it avoids repetition and makes the code reusable.
This is function annotations. It can be use to attach additional information to the arguments or a return values of functions. It is a useful way to say how a function must be used. Functions annotations are stored in a function's __annotations__
attribute.
Use Cases (From documentation)
Providing typing information
Other information
From python-3.5
it can be used for Type Hints
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With