I’m not sure which would correspond to structs/product types, dataclasse
s or TypedDict
s (most probably the former), but with enums/sum types and (abstract) classes, Python is gonna be a way more intuitive and powerful language to model data:
Enums
Now
from enum import Enum
class Colour(Enum):
RED = 1
GREEN = 2
BLUE = 3
New
enum Colour:
Red
Green
Blue(light: bool)
match colour:
case Colour.Red:
print("red")
case Colour.Green:
print("green")
case Colour.Blue(light=false):
print("dark blue")
case Colour.Blue(light=true):
print("light blue")
Structs
Now
from dataclasses import dataclass
@dataclass
class InventoryItem:
name: str
unit_price: float
quantity_on_hand: int = 0
New
struct InventoryItem:
name: str
unit_price: float
quantity_on_hand: int = 0
Abstract classes
Now
from abc import ABC
class Eq[Rhs](ABC):
def __eq__(self, other: Rhs) -> bool:
pass
def __ne__(self, other: Rhs) -> bool:
return not self = other
New
abstract Eq[Rhs]:
def __eq__(self, other: Rhs) -> bool:
pass
def __ne__(self, other: Rhs) -> bool:
return not self = other
or just (:p)
class Eq[Rhs]:
def __eq__(self, other: Rhs) -> bool:
pass
def __ne__(self, other: Rhs) -> bool:
return not self = other
and then
impl Eq for MyType:
...
def eq[T: Eq](a: T, b: T) -> bool:
return a == b
(By the way, can we just terminate declarations by newlines or ;
s, in stead of mandatory pass
s?)
class Eq[Rhs]:
def __eq__(self, other: Rhs) -> bool
def __ne__(self, other: Rhs) -> bool:
return not self = other
or
class Eq[Rhs]:
def __eq__(self, other: Rhs) -> bool;
def __ne__(self, other: Rhs) -> bool:
return not self = other
Macros/metaclasses/decorators
TBD
It’d be worth reconsidering PEP 542 – Dot Notation Assignment In Function Header to deprecate metaclasses.
Thanks.