python: type hinting
This commit is contained in:
parent
1378d3ba67
commit
59beec73f0
1 changed files with 75 additions and 0 deletions
75
Programming_Languages/Python/Concepts/Type_hinting.md
Normal file
75
Programming_Languages/Python/Concepts/Type_hinting.md
Normal file
|
@ -0,0 +1,75 @@
|
||||||
|
---
|
||||||
|
categories:
|
||||||
|
- Programming Languages
|
||||||
|
tags: [python, types]
|
||||||
|
---
|
||||||
|
|
||||||
|
# Type hinting in Python
|
||||||
|
|
||||||
|
With type hinting we can add type information to variables, functions, and classes. This is not enforced by the Python interpreter but can be used by external tools like `mypy` to check the code.
|
||||||
|
|
||||||
|
## Basic syntax
|
||||||
|
|
||||||
|
### Variables
|
||||||
|
|
||||||
|
```py
|
||||||
|
name: str = "John"
|
||||||
|
age: int = 30
|
||||||
|
```
|
||||||
|
|
||||||
|
### Functions
|
||||||
|
|
||||||
|
```py
|
||||||
|
def greet(name: str) -> str:
|
||||||
|
return "Hello, " + name
|
||||||
|
```
|
||||||
|
|
||||||
|
### Complex data types
|
||||||
|
|
||||||
|
// TODO: Give examples of lists, tuples, dictionaries, sets
|
||||||
|
|
||||||
|
### Classes
|
||||||
|
|
||||||
|
// TODO: Add examples
|
||||||
|
|
||||||
|
### Optional types
|
||||||
|
|
||||||
|
`Optional` can be used to indicate that a variable can be `None` or the specified type.
|
||||||
|
|
||||||
|
```py
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
|
def find_index(numbers: List[int], target: int) -> Optional[int]:
|
||||||
|
try:
|
||||||
|
return numbers.index(target)
|
||||||
|
except ValueError:
|
||||||
|
return None
|
||||||
|
```
|
||||||
|
|
||||||
|
The function above returns an `int` or `None`.
|
||||||
|
|
||||||
|
### Union types
|
||||||
|
|
||||||
|
`Union` can be used to indicate that a variable can be one of several types.
|
||||||
|
|
||||||
|
```py
|
||||||
|
from typing import Union
|
||||||
|
|
||||||
|
def add(a: Union[int, float], b: Union[int, float]) -> Union[int, float]:
|
||||||
|
return a + b
|
||||||
|
```
|
||||||
|
|
||||||
|
The function above returns an `int` or `float`.
|
||||||
|
|
||||||
|
> Note that in the case of `Optional` and `Union` the type is specified using square brackets. This is a syntactic covnention and does not denote that the return type is a list. Below is an example of how we would use type hinting to indicate that the return type actually is a list:
|
||||||
|
|
||||||
|
```py
|
||||||
|
from typing import List, Union
|
||||||
|
def my_function() -> List[int]:
|
||||||
|
return [1, 2, 3]
|
||||||
|
|
||||||
|
# And to show a return type of a list of integers or floats:
|
||||||
|
|
||||||
|
def my_function() -> List[Union[int, float]]:
|
||||||
|
return [1, 2, 3]
|
||||||
|
```
|
Loading…
Add table
Reference in a new issue