ALL Python 3.12's major typing improvements

preview_player
Показать описание
Python 3.12 really hit the spot with typing aficionados, so here's a video looking at ALL the major typing improvements made!



If you enjoy my content, consider supporting me on Patreon or becoming a member!

If you need help with anything, feel free to join the Discord server:

I get a lot of people asking, so here's my Visual Studio Code setup!



If you have any questions, don't hesitate to ask in the comments! I'll try and answer as soon as I can, providing someone else hasn't already done so.

#python #coding #howto
Рекомендации по теме
Комментарии
Автор

That was helpful! Python's type system is getting more and more powerful :) Maybe in the future fully typed code could even do something with types to make the code faster as an opt in feature

truthmatters
Автор

Nice explanation. As a side note, return next(iter(seq)) is faster and saves memory. Think of a colossal list. Both versions fail with empty Iterables differently. Your example raises an IndexError, the next(iter(seq)) raises an StopIteration. You could raise in this case an IndexError, maybe. Second thought, perhaps a ValueError. Input is empty, that's bad.

deadeyea
Автор

Why is the return type of `__iter__` None? EDIT: oh fixed it later

notsobob
Автор

I was expecting to hear about 'type' statement.

senyai
Автор

from typing import TypedDict, Unpack, NotRequired

class Kwargs(TypedDict):
name: str
age: NotRequired[int]

def profile(**kwargs: Unpack[Kwargs]):
for k, v in kwargs.items():
print(f"{k}:{v}")

if __name__ == "__main__":
profile(name="Kevin")

from typing import override

class Fruit:
def grow(self):
print("I grew")

def consume(self):
print("Enjoy meal.")

class Banana(Fruit):
@override
def consume(self):
print("Potassium for free")

fruit = Banana()
fruit.consume()

from typing import TypeVar, Generic, Iterable, Iterator

T_co = TypeVar("T_co", covariant=True)

class
def __init__(self, items) -> None:
self.items = items

def __iter__(self) -> Iterator[T_co]:
return iter(self.items)

class Employee:
def __init__(self, name) -> None:
self.name = name

class Manager(Employee):
def __init__(self, name) -> None:
super().__init__(name)

if __name__ == "__main__":
people = ImmutableList([Employee("Kevin"), Manager("Jane")])
print(people)

kvelez
Автор

this is the end of python. whats the point of this type of hacky implementations for type safety although its not safe? just use rust/java etc.
python is trying to be something it never can be. and forgetting its purpose "simplicity".
and no you can't just argue saying its optional. optional feature made it worse

khanra
visit shbcf.ru