Skip to content

Commit 6c3224f

Browse files
committed
Dataclass
1 parent 823f7f6 commit 6c3224f

File tree

2 files changed

+8
-8
lines changed

2 files changed

+8
-8
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -670,7 +670,7 @@ from functools import reduce
670670
['zero', 1, 'zero', 3]
671671
```
672672

673-
### Namedtuple, Enum, Class
673+
### Namedtuple, Enum, Dataclass
674674
```python
675675
from collections import namedtuple
676676
Point = namedtuple('Point', 'x y')
@@ -684,9 +684,9 @@ Cutlery = Enum('Cutlery', {'fork': 1, 'knife': 2, 'spoon': 3})
684684
```
685685

686686
```python
687-
# Warning: Objects will share the objects that are initialized in the dictionary!
688-
Creature = type('Creature', (), {'p': Point(0, 0), 'd': Direction.n})
689-
creature = Creature()
687+
from dataclasses import make_dataclass
688+
Creature = make_dataclass('Creature', ['location', 'direction'])
689+
creature = Creature(Point(0, 0), Direction.n)
690690
```
691691

692692

index.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -620,7 +620,7 @@ <h3 id="ifelse">If - Else</h3>
620620
<pre><code class="python language-python hljs"><span class="hljs-meta">&gt;&gt;&gt; </span>[a <span class="hljs-keyword">if</span> a <span class="hljs-keyword">else</span> <span class="hljs-string">'zero'</span> <span class="hljs-keyword">for</span> a <span class="hljs-keyword">in</span> (<span class="hljs-number">0</span>, <span class="hljs-number">1</span>, <span class="hljs-number">0</span>, <span class="hljs-number">3</span>)]
621621
[<span class="hljs-string">'zero'</span>, <span class="hljs-number">1</span>, <span class="hljs-string">'zero'</span>, <span class="hljs-number">3</span>]
622622
</code></pre>
623-
<h3 id="namedtupleenumclass">Namedtuple, Enum, Class</h3>
623+
<h3 id="namedtupleenumdataclass">Namedtuple, Enum, Dataclass</h3>
624624
<pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> collections <span class="hljs-keyword">import</span> namedtuple
625625
Point = namedtuple(<span class="hljs-string">'Point'</span>, <span class="hljs-string">'x y'</span>)
626626
point = Point(<span class="hljs-number">0</span>, <span class="hljs-number">0</span>)
@@ -629,9 +629,9 @@ <h3 id="namedtupleenumclass">Namedtuple, Enum, Class</h3>
629629
Direction = Enum(<span class="hljs-string">'Direction'</span>, <span class="hljs-string">'n e s w'</span>)
630630
Cutlery = Enum(<span class="hljs-string">'Cutlery'</span>, {<span class="hljs-string">'fork'</span>: <span class="hljs-number">1</span>, <span class="hljs-string">'knife'</span>: <span class="hljs-number">2</span>, <span class="hljs-string">'spoon'</span>: <span class="hljs-number">3</span>})
631631
</code></pre>
632-
<pre><code class="python language-python hljs"><span class="hljs-comment"># Warning: Objects will share the objects that are initialized in the dictionary!</span>
633-
Creature = type(<span class="hljs-string">'Creature'</span>, (), {<span class="hljs-string">'p'</span>: Point(<span class="hljs-number">0</span>, <span class="hljs-number">0</span>), <span class="hljs-string">'d'</span>: Direction.n})
634-
creature = Creature()
632+
<pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> dataclasses <span class="hljs-keyword">import</span> make_dataclass
633+
Creature = make_dataclass(<span class="hljs-string">'Creature'</span>, [<span class="hljs-string">'location'</span>, <span class="hljs-string">'direction'</span>])
634+
creature = Creature(Point(<span class="hljs-number">0</span>, <span class="hljs-number">0</span>), Direction.n)
635635
</code></pre>
636636
<h2 id="closure"><a href="#closure" name="closure">#</a>Closure</h2>
637637
<p><strong>We have a closure in Python when:</strong></p>

0 commit comments

Comments
 (0)