Answers for "python typing optional"

1

python typing optional

from typing import Optional

class Example:
  required_field_1: str
  required_field_2: int
  optional_field: Optional[str]
Posted by: Guest on August-19-2021
3

python typing list of strings

from typing import List

def my_func(l: List[int]):
    pass
Posted by: Guest on October-28-2020
6

typing generator python

# Iterator
def infinite_stream(start: int) -> Iterator[int]:
    while True:
        yield start
        start += 1

# Generator        
def infinite_stream(start: int) -> Generator[int, None, None]:
    while True:
        yield start
        start += 1
Posted by: Guest on February-14-2020
7

python type hint list

# For collections, the name of the type is capitalized, and the
# name of the type inside the collection is in brackets
x: List[int] = [1]
x: Set[int] = {6, 7}
  
# For simple built-in types, just use the name of the type
x: int = 1
x: float = 1.0
x: bool = True
x: str = "test"
x: bytes = b"test"
Posted by: Guest on February-16-2020

Python Answers by Framework

Browse Popular Code Answers by Language