diff --git a/README.md b/README.md index 6869309..5cbe6bd 100644 --- a/README.md +++ b/README.md @@ -871,6 +871,20 @@ class C(A, B): pass [, , , ] ``` +### Dataclass +**Decorator that automatically generates init(), repr() and eq() magic methods. Object can also be made sortable with `'order=True'` and/or immutable with `'frozen=True'`.** +```python +from dataclasses import dataclass, field + +@dataclass(order=False, frozen=False) +class : + : + : = + : list/dict/set = field(default_factory=list/dict/set) +``` +* **Function field() is needed because `': list = []'` would make a list that is shared among all instances.** +* **Default_factory can be any callable.** + ### Copy ```python from copy import copy, deepcopy @@ -918,6 +932,25 @@ class MyHashable: return hash(self.a) ``` +### Sortable +* **With 'total_ordering' decorator you only need to provide one of lt(), gt(), le(), ge() magic methods.** +```python +from functools import total_ordering + +@total_ordering +class MySortable: + def __init__(self, a): + self.a = a + def __eq__(self, other): + if isinstance(other, type(self)): + return self.a == other.a + return NotImplemented + def __lt__(self, other): + if isinstance(other, type(self)): + return self.a < other.a + return NotImplemented +``` + ### Collection * **Methods do not depend on each other, so they can be skipped if not needed.** * **Any object with defined getitem() is considered iterable, even if it lacks iter().** diff --git a/index.html b/index.html index 0a87f2b..fd3f9c8 100644 --- a/index.html +++ b/index.html @@ -776,6 +776,20 @@ creature = Creature(Point(0, >>> C.mro() [<class 'C'>, <class 'A'>, <class 'B'>, <class 'object'>] +

Dataclass

+

Decorator that automatically generates init(), repr() and eq() magic methods. Object can also be made sortable with 'order=True' and/or immutable with 'frozen=True'.

+
from dataclasses import dataclass, field
+
+@dataclass(order=False, frozen=False)
+class <class_name>:
+    <attr_name_1>: <type>
+    <attr_name_2>: <type> = <default_value>
+    <attr_name_3>: list/dict/set = field(default_factory=list/dict/set)
+
+
    +
  • Function field() is needed because '<attr_name>: list = []' would make a list that is shared among all instances.
  • +
  • Default_factory can be any callable.
  • +

Copy

from copy import copy, deepcopy
 <object> = copy(<object>)
@@ -816,6 +830,25 @@ creature  = Creature(Point(0, def __hash__(self):
         return hash(self.a)
 
+

Sortable

+
    +
  • With 'total_ordering' decorator you only need to provide one of lt(), gt(), le(), ge() magic methods.
  • +
+
from functools import total_ordering
+
+@total_ordering
+class MySortable:
+    def __init__(self, a):
+        self.a = a
+    def __eq__(self, other):
+        if isinstance(other, type(self)):
+            return self.a == other.a
+        return NotImplemented
+    def __lt__(self, other):
+        if isinstance(other, type(self)):
+            return self.a < other.a
+        return NotImplemented
+

Collection

  • Methods do not depend on each other, so they can be skipped if not needed.