Skip to content

Commit

Permalink
Update notes
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonas1312 committed Jun 18, 2024
1 parent 9d4094f commit b053977
Showing 1 changed file with 27 additions and 35 deletions.
62 changes: 27 additions & 35 deletions base/science-tech-maths/programming/languages/python/python.md
Original file line number Diff line number Diff line change
Expand Up @@ -313,13 +313,15 @@ from typing import overload


@overload
def double(input_: int) -> int:
... # ellipsis is a placeholder for the actual implementation
def double(
input_: int,
) -> int: ... # ellipsis is a placeholder for the actual implementation


@overload
def double(input_: Sequence[int]) -> list[int]:
... # ellipsis is a placeholder for the actual implementation
def double(
input_: Sequence[int],
) -> list[int]: ... # ellipsis is a placeholder for the actual implementation


def double(input_: int | Sequence[int]) -> int | list[int]:
Expand Down Expand Up @@ -414,16 +416,13 @@ InputType = Literal["TEXT", "IMAGE", "VIDEO"]


# we don't fill the class for this example...
class TextJob:
...
class TextJob: ...


class ImageJob:
...
class ImageJob: ...


class VideoJob:
...
class VideoJob: ...


T = TypeVar("T", TextJob, ImageJob, VideoJob)
Expand All @@ -434,16 +433,17 @@ class MyMainClass(Generic[T]):

# we use overload to define the different signatures of the __init__ method
@overload
def __init__(self: "MyMainClass[TextJob]", input_type: Literal["TEXT"]) -> None:
...
def __init__(self: "MyMainClass[TextJob]", input_type: Literal["TEXT"]) -> None: ...

@overload
def __init__(self: "MyMainClass[ImageJob]", input_type: Literal["IMAGE"]) -> None:
...
def __init__(
self: "MyMainClass[ImageJob]", input_type: Literal["IMAGE"]
) -> None: ...

@overload
def __init__(self: "MyMainClass[VideoJob]", input_type: Literal["VIDEO"]) -> None:
...
def __init__(
self: "MyMainClass[VideoJob]", input_type: Literal["VIDEO"]
) -> None: ...

# we usually start with the "real" implementation and then we define the overload above
def __init__(self, input_type: InputType) -> None:
Expand Down Expand Up @@ -483,21 +483,18 @@ if TYPE_CHECKING:
import pandas as pd


def export_as_df() -> pd.DataFrame:
...
def export_as_df() -> pd.DataFrame: ...
```

### Covariance and contravariance

#### Covariance: `CovariantType[SubType, ...] <: CovariantType[SuperType, ...]`

```python
class Animal:
...
class Animal: ...


class Dog(Animal):
... # Dog <: Animal, "Dog is a subtype of Animal"
class Dog(Animal): ... # Dog <: Animal, "Dog is a subtype of Animal"


an_animal = Animal()
Expand Down Expand Up @@ -526,16 +523,13 @@ In python, most containers are covariant:
The definition is almost the same as of “covariance”, but with <: relation switched.

```python
class Animal:
...
class Animal: ...


class Dog(Animal):
...
class Dog(Animal): ...


class Kangaroo(Animal):
...
class Kangaroo(Animal): ...


# this function type is `Callable[[Animal], None]`
Expand Down Expand Up @@ -964,8 +958,7 @@ Note that with `@contextmanager` your context manager can also be used as a deco

```python
@my_context_manager
def my_function():
...
def my_function(): ...
```

It is possible to open multiple contexts at once:
Expand Down Expand Up @@ -1697,9 +1690,9 @@ MagicMock is a subclass of Mock with default implementations of most of the magi

```python
mock = MagicMock()
mock[
3
] = "fish" # MagicMock already has __getitem__ implemented, this would crash with Mock
mock[3] = (
"fish" # MagicMock already has __getitem__ implemented, this would crash with Mock
)
mock.__setitem__.assert_called_with(3, "fish") # true
mock.__getitem__.return_value = "result"
mock[2] # 'result'
Expand Down Expand Up @@ -2022,8 +2015,7 @@ There are three main types of awaitable objects:
1. coroutines

```python
async def some_coroutine():
...
async def some_coroutine(): ...
```

2. Tasks
Expand Down

0 comments on commit b053977

Please sign in to comment.