Skip to content

Commit c637f0f

Browse files
committed
Added slots
1 parent ebe487c commit c637f0f

File tree

3 files changed

+16
-0
lines changed

3 files changed

+16
-0
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1033,6 +1033,15 @@ class <class_name>:
10331033
* **Function field() is needed because `'<attr_name>: list = []'` would make a list that is shared among all instances.**
10341034
* **Default_factory can be any callable.**
10351035

1036+
### Slots
1037+
**Mechanism that restricts objects to attributes listed in 'slots' and significantly reduces their memory footprint.**
1038+
1039+
```
1040+
class MyClassWithSlots:
1041+
__slots__ = ['a']
1042+
def __init__(self):
1043+
self.a = 1
1044+
```
10361045

10371046
### Copy
10381047
```python

index.html

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -974,6 +974,13 @@ <h3 id="dataclass">Dataclass</h3>
974974
<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>
975975
<li><strong>Default_factory can be any callable.</strong></li>
976976
</ul>
977+
<h3 id="slots">Slots</h3>
978+
<p><strong>Mechanism that restricts objects to attributes listed in 'slots' and significantly reduces their memory footprint.</strong></p>
979+
<pre><code class="python hljs"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MyClassWithSlots</span>:</span>
980+
__slots__ = [<span class="hljs-string">'a'</span>]
981+
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self)</span>:</span>
982+
self.a = <span class="hljs-number">1</span>
983+
</code></pre>
977984
<h3 id="copy">Copy</h3>
978985
<pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> copy <span class="hljs-keyword">import</span> copy, deepcopy
979986
&lt;object&gt; = copy(&lt;object&gt;)

web/image_social.png

182 KB
Loading

0 commit comments

Comments
 (0)