Browse Source

Inline

pull/135/head
Jure Šorn 2 years ago
parent
commit
fe9a0d123f
2 changed files with 12 additions and 12 deletions
  1. 12
      README.md
  2. 12
      index.html

12
README.md

@ -796,20 +796,20 @@ Inline
### Named Tuple, Enum, Dataclass
```python
from collections import namedtuple
Point = namedtuple('Point', 'x y') # Tuple's subclass with named elements.
point = Point(0, 0) # Tuple with x and y attributes.
Point = namedtuple('Point', 'x y') # Creates a tuple's subclass.
point = Point(0, 0) # Returns its instance.
```
```python
from enum import Enum
Direction = Enum('Direction', 'n e s w') # Enum with n, e, s and w members.
direction = Direction.n # Member with name and value attributes.
Direction = Enum('Direction', 'n e s w') # Creates an enum.
direction = Direction.n # Returns its member.
```
```python
from dataclasses import make_dataclass
Player = make_dataclass('Player', ['loc', 'dir']) # Class with init, repr and eq methods.
player = Player(point, direction) # Object with loc and dir attributes.
Player = make_dataclass('Player', ['loc', 'dir']) # Creates a class.
player = Player(point, direction) # Returns its instance.
```

12
index.html

@ -680,17 +680,17 @@ func(*args, **kwargs)
[<span class="hljs-string">'zero'</span>, <span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>]
</code></pre>
<div><h3 id="namedtupleenumdataclass">Named Tuple, Enum, Dataclass</h3><pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> collections <span class="hljs-keyword">import</span> namedtuple
Point = namedtuple(<span class="hljs-string">'Point'</span>, <span class="hljs-string">'x y'</span>) <span class="hljs-comment"># Tuple's subclass with named elements.</span>
point = Point(<span class="hljs-number">0</span>, <span class="hljs-number">0</span>) <span class="hljs-comment"># Tuple with x and y attributes.</span>
Point = namedtuple(<span class="hljs-string">'Point'</span>, <span class="hljs-string">'x y'</span>) <span class="hljs-comment"># Creates a tuple's subclass.</span>
point = Point(<span class="hljs-number">0</span>, <span class="hljs-number">0</span>) <span class="hljs-comment"># Returns its instance.</span>
</code></pre></div>
<pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> enum <span class="hljs-keyword">import</span> Enum
Direction = Enum(<span class="hljs-string">'Direction'</span>, <span class="hljs-string">'n e s w'</span>) <span class="hljs-comment"># Enum with n, e, s and w members.</span>
direction = Direction.n <span class="hljs-comment"># Member with name and value attributes.</span>
Direction = Enum(<span class="hljs-string">'Direction'</span>, <span class="hljs-string">'n e s w'</span>) <span class="hljs-comment"># Creates an enum.</span>
direction = Direction.n <span class="hljs-comment"># Returns its member.</span>
</code></pre>
<pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> dataclasses <span class="hljs-keyword">import</span> make_dataclass
Player = make_dataclass(<span class="hljs-string">'Player'</span>, [<span class="hljs-string">'loc'</span>, <span class="hljs-string">'dir'</span>]) <span class="hljs-comment"># Class with init, repr and eq methods.</span>
player = Player(point, direction) <span class="hljs-comment"># Object with loc and dir attributes.</span>
Player = make_dataclass(<span class="hljs-string">'Player'</span>, [<span class="hljs-string">'loc'</span>, <span class="hljs-string">'dir'</span>]) <span class="hljs-comment"># Creates a class.</span>
player = Player(point, direction) <span class="hljs-comment"># Returns its instance.</span>
</code></pre>
<div><h2 id="imports"><a href="#imports" name="imports">#</a>Imports</h2><pre><code class="python language-python hljs"><span class="hljs-keyword">import</span> &lt;module&gt; <span class="hljs-comment"># Imports a built-in or '&lt;module&gt;.py'.</span>
<span class="hljs-keyword">import</span> &lt;package&gt; <span class="hljs-comment"># Imports a built-in or '&lt;package&gt;/__init__.py'.</span>

Loading…
Cancel
Save