Created
July 18, 2018 21:17
-
-
Save tsunday/f39b64bb3b5b4f736a26be5908c8bfb2 to your computer and use it in GitHub Desktop.
Playing around with a new dataclasses module
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from dataclasses import dataclass, field, asdict | |
@dataclass(order=True) | |
class City: | |
name: str = field(compare=False) | |
citizens: int | |
area: float | |
zamosc = City('Zamość', 65, 30) | |
krakow = City('Kraków', 767, 326) | |
ochock = City('Ochock', 3, 400) | |
bigger = zamosc if zamosc > krakow else krakow | |
print(f'Bigger city: {bigger}') | |
print(sorted([zamosc, krakow, ochock])) | |
@dataclass | |
class A: | |
x: int = 1 | |
y: int = 2 | |
@dataclass | |
class B(A): | |
z: int = 3 | |
x: int = 5 | |
print(A()) | |
print(B()) | |
print(B(0, 1, 2)) | |
@dataclass | |
class Product: | |
size: int | |
quantity: int | |
price: float | |
p = Product(10, 1, 1.23) | |
print(asdict(p)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment