Dataclasses aren't orderable by default. Add `order=True` to auto-generate comparison methods like __lt__ & __gt__.
This makes sorting/compare tasks seamless! Check out an example 👇
How do you use dataclasses in your projects? 🚀
Dataclasses aren't orderable by default. Add `order=True` to auto-generate comparison methods like __lt__ & __gt__.
This makes sorting/compare tasks seamless! Check out an example 👇
How do you use dataclasses in your projects? 🚀
Let dataclasses write your dunder methods. 🧵
The dataclass decorator writes __init__, __repr__, and __eq__ for you:
from dataclasses import dataclass
@dataclass
class Comment:
text: str
line: int
#Python #DailyPythonTip
Let dataclasses write your dunder methods. 🧵
The dataclass decorator writes __init__, __repr__, and __eq__ for you:
from dataclasses import dataclass
@dataclass
class Comment:
text: str
line: int
#Python #DailyPythonTip
from dataclasses import dataclass
@ dataclass
class Person:
gender:Any = None
from dataclasses import dataclass
@ dataclass
class Person:
gender:Any = None
- Clean, decomposable interfaces that cut the system at its joints
- Documentation for each module, explaining what it contains and where
- Documentation of decisions made, and decisions *not* made and why. /.
- Clean, decomposable interfaces that cut the system at its joints
- Documentation for each module, explaining what it contains and where
- Documentation of decisions made, and decisions *not* made and why. /.
1. use type annotations by default. Will make it MUCH easier to debug later on.
2. variable names: t_transpose bad, explain what it means
3. PyTorch shape suffixes: good idea medium.com/@NoamShazeer...
4. enums good, dataclasses great
1. use type annotations by default. Will make it MUCH easier to debug later on.
2. variable names: t_transpose bad, explain what it means
3. PyTorch shape suffixes: good idea medium.com/@NoamShazeer...
4. enums good, dataclasses great
Customizing dataclass fields & initialization sometimes takes a bit of work and inheriting from a dataclasses can be a BIT odd...
Customizing dataclass fields & initialization sometimes takes a bit of work and inheriting from a dataclasses can be a BIT odd...
Sort by specific attributes with operator.attrgetter 🧵
Have an iterable of dataclasses, namedtuples, or some other record-like object?
Want to sort them by a specific attribute?
Use operator.attrgetter:
from operator import attrgetter
#Python #DailyPythonTip
Sort by specific attributes with operator.attrgetter 🧵
Have an iterable of dataclasses, namedtuples, or some other record-like object?
Want to sort them by a specific attribute?
Use operator.attrgetter:
from operator import attrgetter
#Python #DailyPythonTip
Im always convincing folks to use torch/jax typing & dataclasses — because I do not have the mental bandwidth to remember what is in any given container variable.
Im always convincing folks to use torch/jax typing & dataclasses — because I do not have the mental bandwidth to remember what is in any given container variable.