Browse Source

Dataclass

pull/45/head
Jure Šorn 5 years ago
parent
commit
65842dc0e4
2 changed files with 4 additions and 2 deletions
  1. 3
      README.md
  2. 3
      index.html

3
README.md

@ -872,7 +872,7 @@ class C(A, B): pass
``` ```
### Dataclass ### 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'`.**
**Decorator that automatically generates init(), repr() and eq() magic methods.**
```python ```python
from dataclasses import dataclass, field from dataclasses import dataclass, field
@ -882,6 +882,7 @@ class <class_name>:
<attr_name_2>: <type> = <default_value> <attr_name_2>: <type> = <default_value>
<attr_name_3>: list/dict/set = field(default_factory=list/dict/set) <attr_name_3>: list/dict/set = field(default_factory=list/dict/set)
``` ```
* **An object can be made sortable with `'order=True'` or immutable with `'frozen=True'`.**
* **Function field() is needed because `'<attr_name>: list = []'` would make a list that is shared among all instances.** * **Function field() is needed because `'<attr_name>: list = []'` would make a list that is shared among all instances.**
* **Default_factory can be any callable.** * **Default_factory can be any callable.**

3
index.html

@ -777,7 +777,7 @@ creature = Creature(Point(<span class="hljs-number">0</span>, <span class="hljs
[&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;] [&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;]
</span></code></pre> </span></code></pre>
<h3 id="dataclass">Dataclass</h3> <h3 id="dataclass">Dataclass</h3>
<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>
<p><strong>Decorator that automatically generates init(), repr() and eq() magic methods.</strong></p>
<pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> dataclasses <span class="hljs-keyword">import</span> dataclass, field <pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> dataclasses <span class="hljs-keyword">import</span> dataclass, field
<span class="hljs-meta">@dataclass(order=False, frozen=False)</span> <span class="hljs-meta">@dataclass(order=False, frozen=False)</span>
@ -787,6 +787,7 @@ creature = Creature(Point(<span class="hljs-number">0</span>, <span class="hljs
&lt;attr_name_3&gt;: list/dict/set = field(default_factory=list/dict/set) &lt;attr_name_3&gt;: list/dict/set = field(default_factory=list/dict/set)
</code></pre> </code></pre>
<ul> <ul>
<li><strong>An object can be made sortable with <code class="python hljs"><span class="hljs-string">'order=True'</span></code> or immutable with <code class="python hljs"><span class="hljs-string">'frozen=True'</span></code>.</strong></li>
<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> <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>
<li><strong>Default_factory can be any callable.</strong></li> <li><strong>Default_factory can be any callable.</strong></li>
</ul> </ul>

Loading…
Cancel
Save