mirror of
https://github.com/Brandon-Rozek/website.git
synced 2025-10-10 06:51:13 +00:00
New Posts
This commit is contained in:
parent
a650fc940e
commit
2cc490f3f2
3 changed files with 130 additions and 0 deletions
29
content/blog/pydataclass.md
Normal file
29
content/blog/pydataclass.md
Normal file
|
@ -0,0 +1,29 @@
|
|||
---
|
||||
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
|
||||
```
|
||||
|
||||
Usage:
|
||||
|
||||
```python
|
||||
p = Person("Bob", 30)
|
||||
print(p)
|
||||
```
|
||||
|
||||
```
|
||||
Person(name='Bob', age=20)
|
||||
```
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue