Modern Python ?. Alla scoperta delle ultime novità di (C)Python.
About me. ? Studente di Informatica (1° anno) ? Freelancer occasionale ? Appassionato di Python.
Punti salienti ?. Tips & Tricks __format__ __matmul__ __reversed__ functools.lru_cache contextlib.suppress contextlib.contextmanager (+ async) Type hinting Pathlib Generatori Async Pattern Matching Walrus Operator.
Tips & Tricks ✨.
__format__. Gestione dei format specifier Chiamato ad ogni format() /f-string Format specifier personalizzati.
__matmul__. Operatore di moltiplicazione matriciale Origine: NumPy Codice più conciso Evitare di abusarne se si tiene al posto di lavoro!.
__reversed__. Iterazione inversa tramite generatori: Non induce copie [::-1] è oscuro, reversed() è esplicito Nota : Non funziona con i generatori!.
@lru_cache. Minimalmente intrusivo Estremamente efficace Nota: Esiste anche functools. cache senza il meccanismo LRU!.
Memoizzazione con @lru_cache — Dimostrazione.
Non elegante Verboso Non flessibile Breve Conciso Esplicito.
Più conciso rispetto a due metodi __enter__ ed __exit__ Supporto async: contextlib.asynccontextmanager.
Type Hinting. MY NEW 15GREAT, FS A REGARDING T,'Æ: > I10.5 (10) > 03] > 2+7 (2/0) NAN (2/0)+2 NAP FRISE TRUE NAN.ooonDØC013 RANGE(Ø 12 2+2 FLOOR(lO.5).
Type Hinting - Unioni (prima). ✍️ Lungo da scrivere ? Verboso.
Type Hinting - Unioni (dopo). ? Breve ✨ Elegante ? P yth onico! Nota : Richiede Python 3.10.
Type Hinting - Alias. Type hint: opzionale Miglior supporto ai type checker Più elegante.
Type Hinting - Generic alias. Note : Non possono essere passati a i sinstance() Python non controlla il tipo dei valori a runtime repr() e str() mostrano gli argomenti generici.
Type Hints - Coroutines (Bonus!). import asyncio async def coro(name: str, delay: int) -> None: is sleeping for seconds! " ) await asyncio.sleep(delay) # Callable[[str, int], Coroutine[Any, Any, Any]].
Pathlib. 291.dcc Untitled 136 l)nidkd 13B Qb2.docx Ontdled 139. cbcx Vnttld qo ADRß5.jf3 292e Unt;tld 2143.Joc Untitled 2q3 OHMY@ NEVER IN a-SE's ØCLMENTS.
File Handling - Alla vecchia maniera ?. TART! AFTER TÆ ØLER? THItKT<RE'S NO ACTURL filEsx'STB•1S. ITS SO TAO!.
File Handling - Il futuro è qui ?. ? Conciso ✨ Elegante ? Nella stdlib.
Generatori Async ⚙️.
import time def ticker(to: tnt, delay: for t in range(to): yield i time . sleep( delay ) for t in delay= print(i) tnt): 1):.
import time import threading def ticker(to: int, delay: for in range(to): yield time. sleep(delay) int): def main( ) : for t in ticker (10, print(i) delay= 1): threading . Thread( target=matn) . run( ).
import asyncio async def ticker (to: tnt, delay: tnt): for t in range(to): yield await asyncto.sleep(delay) async def main( ) : async for in ticker(10, print(t) asyncto. ) ) delay—I ) :.
Pattern Matching.
Pattern Matching (prima). Verboso Poco Elegante Ripetitivo.
Pattern Matching (dopo). Conciso Elegante Flessibile Non supporta espressioni:.
Pattern Matching - Bonus ?. match event. get( ) : case handle_click_at(x, y) case KeyPress( ) I Quit(): game. quit( ) case KeyPress( arrow" ) : game. go _ north( ) case KeyPress( ) : pass # Ignore other keystrokes case other event: raise ValueError(f"Unrecognized event: " ).
E infine … Il tricheco!. Raffinato ? Conciso.
Occhio alle parentesi!*. *Nessun tricheco è stato maltrattato per la realizzazione di questa presentazione.
Grazie per aver partecipato! ?. https://nocturn9x.space.