alexey-goloburdin / typed-python-book Goto Github PK
View Code? Open in Web Editor NEWLicense: Other
License: Other
Цитата из главы 2.6:
Не нужно использовать ООП просто для того, чтобы у вас были классы.
Возьмём код
def save_weather(weather: Weather, storage: WeatherStorage) -> None:
"""Saves weather in the storage"""
storage.save(weather)
Если мы хотим, чтобы storage был функцией, то код будет такой:
WeatherStorage = Callable[[Weather], None]
def save_weather(weather: Weather, storage: WeatherStorage) -> None:
"""Saves weather in the storage"""
storage(weather)
Add small section about type: ignore
.
For example code really works, but fails with mypy
(and works with pyright
):
from typing import Sequence
from datetime import datetime
from dataclasses import dataclass
@dataclass
class User:
birthday: datetime
class Users:
def __init__(self, users: Sequence[User]):
self._users = users
def __getitem__(self, key: int) -> User:
return self._users[key]
users = Users(( # сменили на tuple
User(birthday=datetime.fromisoformat("1988-01-01")),
User(birthday=datetime.fromisoformat("1985-07-29")),
User(birthday=datetime.fromisoformat("2000-10-10"))
))
for u in users:
print(u)
Пользователь будет вытягивать образ из докерхаба, а разработчик собирать образ и пушить в докерхаб.
self._some_dict: dict[str, int] = {}
- читается достаточно легко
self._some_dict: dict[Coordinates, Celsius] = {}
- Логика теряется на фоне описания типов
self._some_dict = {} # type: dict[Coordinates, Celsius]
- логика отдельно, мухи описание отдельно
Ещё такой подход помогает в циклах:
Нельзя написать for index:int, value:int in enumerate(data):
Но можно написать так:
data = load.jsons(dump)
for index, value in enumerate(data): # type: int, int
...
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.