mirror of
https://github.com/Brandon-Rozek/website.git
synced 2024-11-28 15:23:40 -05:00
31 lines
600 B
Markdown
31 lines
600 B
Markdown
---
|
|
title: "Quick Python: Dataclasses"
|
|
date: 2020-04-08T18:59:48-04:00
|
|
draft: false
|
|
tags: ["Python"]
|
|
---
|
|
|
|
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.
|
|
|
|
```python
|
|
from dataclasses import dataclass
|
|
|
|
@dataclass
|
|
class Person:
|
|
name: str
|
|
age: int = 0
|
|
```
|
|
|
|
Usage:
|
|
|
|
```python
|
|
p = Person("Bob", 30)
|
|
print(p)
|
|
```
|
|
|
|
```
|
|
Person(name='Bob', age=20)
|
|
```
|
|
|
|
To make an attribute have a default value, add it after the type declaration like I have with `age`.
|
|
|