python: further notes on type hinting
This commit is contained in:
parent
a1a908261c
commit
0d1e2b5688
1 changed files with 23 additions and 6 deletions
|
@ -24,15 +24,32 @@ def greet(name: str) -> str:
|
||||||
return "Hello, " + name
|
return "Hello, " + name
|
||||||
```
|
```
|
||||||
|
|
||||||
### Complex data types
|
|
||||||
|
|
||||||
// TODO: Give examples of lists, tuples, dictionaries, sets
|
|
||||||
|
|
||||||
### Classes
|
### Classes
|
||||||
|
|
||||||
// TODO: Add examples
|
// TODO: Add examples
|
||||||
|
|
||||||
### Optional types
|
## Complex data types
|
||||||
|
|
||||||
|
// TODO: Give examples of lists, tuples, dictionaries, sets
|
||||||
|
|
||||||
|
## Custom dictionay types
|
||||||
|
|
||||||
|
We can create custom dictionay types in the manner of TypeScript types and interfaces as follows:
|
||||||
|
|
||||||
|
```py
|
||||||
|
class ArticleInfo(TypedDict):
|
||||||
|
timestamp: str
|
||||||
|
article_title: str
|
||||||
|
link: str
|
||||||
|
```
|
||||||
|
|
||||||
|
We could then use it thus:
|
||||||
|
|
||||||
|
```py
|
||||||
|
def parse_articles() -> List[ArticleInfo]:
|
||||||
|
```
|
||||||
|
|
||||||
|
## Optional types
|
||||||
|
|
||||||
`Optional` can be used to indicate that a variable can be `None` or the specified type.
|
`Optional` can be used to indicate that a variable can be `None` or the specified type.
|
||||||
|
|
||||||
|
@ -48,7 +65,7 @@ def find_index(numbers: List[int], target: int) -> Optional[int]:
|
||||||
|
|
||||||
The function above returns an `int` or `None`.
|
The function above returns an `int` or `None`.
|
||||||
|
|
||||||
### Union types
|
## Union types
|
||||||
|
|
||||||
`Union` can be used to indicate that a variable can be one of several types.
|
`Union` can be used to indicate that a variable can be one of several types.
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue