mirror of
https://github.com/Brandon-Rozek/website.git
synced 2024-11-09 18:50:34 -05:00
494 B
494 B
title | date | draft | tags | |
---|---|---|---|---|
Quick Python: Dataclasses | 2020-04-08T18:59:48-04:00 | false |
|
Python 3.7 and above have a feature called dataclasses. This allows us to reduce boilerplate code by removing the need to create a whole constructor and providing a sensible __repr__
function.
from dataclasses import dataclass
@dataclass
class Person:
name: str
age: int
Usage:
p = Person("Bob", 30)
print(p)
Person(name='Bob', age=20)