Browse Source

Property

pull/33/head
Jure Šorn 5 years ago
parent
commit
6b840060f3
2 changed files with 39 additions and 4 deletions
  1. 24
      README.md
  2. 19
      index.html

24
README.md

@ -871,6 +871,25 @@ class C(A, B): pass
[<class 'C'>, <class 'A'>, <class 'B'>, <class 'object'>] [<class 'C'>, <class 'A'>, <class 'B'>, <class 'object'>]
``` ```
### Property
```python
class MyClass:
@property
def a(self):
return self._a
@a.setter
def a(self, value):
self._a = value
```
```python
>>> el = MyClass()
>>> el.a = 123
>>> el.a
123
```
### Dataclass ### Dataclass
**Decorator that automatically generates init(), repr() and eq() special methods.** **Decorator that automatically generates init(), repr() and eq() special methods.**
```python ```python
@ -886,6 +905,7 @@ class <class_name>:
* **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.**
### Copy ### Copy
```python ```python
from copy import copy, deepcopy from copy import copy, deepcopy
@ -921,10 +941,10 @@ class MyComparable:
```python ```python
class MyHashable: class MyHashable:
def __init__(self, a): def __init__(self, a):
self.__a = copy.deepcopy(a)
self._a = copy.deepcopy(a)
@property @property
def a(self): def a(self):
return self.__a
return self._a
def __eq__(self, other): def __eq__(self, other):
if isinstance(other, type(self)): if isinstance(other, type(self)):
return self.a == other.a return self.a == other.a

19
index.html

@ -823,6 +823,21 @@ creature = Creature(Point(<span class="hljs-number">0</span>, <span class="hljs
<pre><code class="python language-python hljs"><span class="hljs-meta">&gt;&gt;&gt; </span>C.mro() <pre><code class="python language-python hljs"><span class="hljs-meta">&gt;&gt;&gt; </span>C.mro()
[&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="property">Property</h3>
<pre><code class="python language-python hljs"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MyClass</span>:</span>
<span class="hljs-meta"> @property</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">a</span><span class="hljs-params">(self)</span>:</span>
<span class="hljs-keyword">return</span> self._a
<span class="hljs-meta"> @a.setter</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">a</span><span class="hljs-params">(self, value)</span>:</span>
self._a = value
</code></pre>
<pre><code class="python language-python hljs"><span class="hljs-meta">&gt;&gt;&gt; </span>el = MyClass()
<span class="hljs-meta">&gt;&gt;&gt; </span>el.a = <span class="hljs-number">123</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>el.a
<span class="hljs-number">123</span>
</code></pre>
<h3 id="dataclass">Dataclass</h3> <h3 id="dataclass">Dataclass</h3>
<p><strong>Decorator that automatically generates init(), repr() and eq() special methods.</strong></p> <p><strong>Decorator that automatically generates init(), repr() and eq() special 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
@ -867,10 +882,10 @@ creature = Creature(Point(<span class="hljs-number">0</span>, <span class="hljs
</ul> </ul>
<pre><code class="python language-python hljs"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MyHashable</span>:</span> <pre><code class="python language-python hljs"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MyHashable</span>:</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self, a)</span>:</span> <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self, a)</span>:</span>
self.__a = copy.deepcopy(a)
self._a = copy.deepcopy(a)
<span class="hljs-meta"> @property</span> <span class="hljs-meta"> @property</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">a</span><span class="hljs-params">(self)</span>:</span> <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">a</span><span class="hljs-params">(self)</span>:</span>
<span class="hljs-keyword">return</span> self.__a
<span class="hljs-keyword">return</span> self._a
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__eq__</span><span class="hljs-params">(self, other)</span>:</span> <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__eq__</span><span class="hljs-params">(self, other)</span>:</span>
<span class="hljs-keyword">if</span> isinstance(other, type(self)): <span class="hljs-keyword">if</span> isinstance(other, type(self)):
<span class="hljs-keyword">return</span> self.a == other.a <span class="hljs-keyword">return</span> self.a == other.a

Loading…
Cancel
Save