Skip to content

Commit c1b93d3

Browse files
committed
Dataclass, sortable
1 parent d76b3b6 commit c1b93d3

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -871,6 +871,20 @@ class C(A, B): pass
871871
[<class 'C'>, <class 'A'>, <class 'B'>, <class 'object'>]
872872
```
873873

874+
### Dataclass
875+
**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'`.**
876+
```python
877+
from dataclasses import dataclass, field
878+
879+
@dataclass(order=False, frozen=False)
880+
class <class_name>:
881+
<attr_name_1>: <type>
882+
<attr_name_2>: <type> = <default_value>
883+
<attr_name_3>: list/dict/set = field(default_factory=list/dict/set)
884+
```
885+
* **Function field() is needed because `'<attr_name>: list = []'` would make a list that is shared among all instances.**
886+
* **Default_factory can be any callable.**
887+
874888
### Copy
875889
```python
876890
from copy import copy, deepcopy
@@ -918,6 +932,25 @@ class MyHashable:
918932
return hash(self.a)
919933
```
920934

935+
### Sortable
936+
* **With 'total_ordering' decorator you only need to provide one of lt(), gt(), le(), ge() magic methods.**
937+
```python
938+
from functools import total_ordering
939+
940+
@total_ordering
941+
class MySortable:
942+
def __init__(self, a):
943+
self.a = a
944+
def __eq__(self, other):
945+
if isinstance(other, type(self)):
946+
return self.a == other.a
947+
return NotImplemented
948+
def __lt__(self, other):
949+
if isinstance(other, type(self)):
950+
return self.a < other.a
951+
return NotImplemented
952+
```
953+
921954
### Collection
922955
* **Methods do not depend on each other, so they can be skipped if not needed.**
923956
* **Any object with defined getitem() is considered iterable, even if it lacks iter().**

index.html

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -776,6 +776,20 @@ <h3 id="multipleinheritance">Multiple Inheritance</h3>
776776
<pre><code class="python language-python hljs"><span class="hljs-meta">&gt;&gt;&gt; </span>C.mro()
777777
[&lt;<span class="hljs-class"><span class="hljs-title">class</span> '<span class="hljs-title">C</span>'&gt;, &lt;<span class="hljs-title">class</span> '<span class="hljs-title">A</span>'&gt;, &lt;<span class="hljs-title">class</span> '<span class="hljs-title">B</span>'&gt;, &lt;<span class="hljs-title">class</span> '<span class="hljs-title">object</span>'&gt;]
778778
</span></code></pre>
779+
<h3 id="dataclass">Dataclass</h3>
780+
<p><strong>Decorator that automatically generates init(), repr() and eq() magic methods. Object can also be made sortable with <code class="python hljs"><span class="hljs-string">'order=True'</span></code> and/or immutable with <code class="python hljs"><span class="hljs-string">'frozen=True'</span></code>.</strong></p>
781+
<pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> dataclasses <span class="hljs-keyword">import</span> dataclass, field
782+
783+
<span class="hljs-meta">@dataclass(order=False, frozen=False)</span>
784+
<span class="hljs-class"><span class="hljs-keyword">class</span> &lt;<span class="hljs-title">class_name</span>&gt;:</span>
785+
&lt;attr_name_1&gt;: &lt;type&gt;
786+
&lt;attr_name_2&gt;: &lt;type&gt; = &lt;default_value&gt;
787+
&lt;attr_name_3&gt;: list/dict/set = field(default_factory=list/dict/set)
788+
</code></pre>
789+
<ul>
790+
<li><strong>Function field() is needed because <code class="python hljs"><span class="hljs-string">'&lt;attr_name&gt;: list = []'</span></code> would make a list that is shared among all instances.</strong></li>
791+
<li><strong>Default_factory can be any callable.</strong></li>
792+
</ul>
779793
<h3 id="copy">Copy</h3>
780794
<pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> copy <span class="hljs-keyword">import</span> copy, deepcopy
781795
&lt;object&gt; = copy(&lt;object&gt;)
@@ -816,6 +830,25 @@ <h3 id="hashable">Hashable</h3>
816830
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__hash__</span><span class="hljs-params">(self)</span>:</span>
817831
<span class="hljs-keyword">return</span> hash(self.a)
818832
</code></pre>
833+
<h3 id="sortable">Sortable</h3>
834+
<ul>
835+
<li><strong>With 'total_ordering' decorator you only need to provide one of lt(), gt(), le(), ge() magic methods.</strong></li>
836+
</ul>
837+
<pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> functools <span class="hljs-keyword">import</span> total_ordering
838+
839+
<span class="hljs-meta">@total_ordering</span>
840+
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MySortable</span>:</span>
841+
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self, a)</span>:</span>
842+
self.a = a
843+
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__eq__</span><span class="hljs-params">(self, other)</span>:</span>
844+
<span class="hljs-keyword">if</span> isinstance(other, type(self)):
845+
<span class="hljs-keyword">return</span> self.a == other.a
846+
<span class="hljs-keyword">return</span> <span class="hljs-built_in">NotImplemented</span>
847+
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__lt__</span><span class="hljs-params">(self, other)</span>:</span>
848+
<span class="hljs-keyword">if</span> isinstance(other, type(self)):
849+
<span class="hljs-keyword">return</span> self.a &lt; other.a
850+
<span class="hljs-keyword">return</span> <span class="hljs-built_in">NotImplemented</span>
851+
</code></pre>
819852
<h3 id="collection">Collection</h3>
820853
<ul>
821854
<li><strong>Methods do not depend on each other, so they can be skipped if not needed.</strong></li>

0 commit comments

Comments
 (0)