Muestra las diferencias entre dos versiones de la página.
| Ambos lados, revisión anterior Revisión previa Próxima revisión | Revisión previa | ||
|
wiki2:python:typing [2022/03/14 17:42] alfred |
wiki2:python:typing [2022/10/12 19:03] (actual) |
||
|---|---|---|---|
| Línea 17: | Línea 17: | ||
| </code> | </code> | ||
| - | Definición de un tipo: | + | Definición de un tipo (en este caso self): |
| <code> | <code> | ||
| from typing import TypeVar | from typing import TypeVar | ||
| Línea 25: | Línea 25: | ||
| class Foo: | class Foo: | ||
| def returns_self(self: Self) -> Self: | def returns_self(self: Self) -> Self: | ||
| - | ... | ||
| return self | return self | ||
| </code> | </code> | ||
| + | |||
| + | Several types: | ||
| + | <code> | ||
| + | Union[Union[int, str], float] == Union[int, str, float] | ||
| + | Union[int] == int | ||
| + | </code> | ||
| + | |||
| + | Type or None: | ||
| + | ''Optional[X]'' is equivalent to ''X | None'' (or ''Union[X, None]''). | ||
| + | <code> | ||
| + | Optional[str] | ||
| + | </code> | ||
| + | |||
| + | Class type: | ||
| + | <code> | ||
| + | class ShopifyPostView(MethodView): | ||
| + | shopify_request_schema: t.Union[ | ||
| + | t.Type[ShopifyPaymentsRequest], | ||
| + | t.Type[ShopifyRefundsRequest], | ||
| + | ] = None | ||
| + | </code> | ||
| + | |||
| + | Object of the previous class type: | ||
| + | <code> | ||
| + | ShopifyRequestClass: t.Type = t.Optional[ | ||
| + | t.Union[ | ||
| + | t.Type[ShopifyPaymentsRequest], | ||
| + | t.Type[ShopifyRefundsRequest], | ||
| + | ] | ||
| + | ] | ||
| + | |||
| + | ShopifyRequest = t.Type[ShopifyRequestClass] | ||
| + | </code> | ||
| + | |||
| + | ====== Links ====== | ||
| + | |||
| + | * https://towardsdatascience.com/12-beginner-concepts-about-type-hints-to-improve-your-python-code-90f1ba0ac49 | ||