Skip to content

Commit 6b84006

Browse files
committed
Property
1 parent 528d617 commit 6b84006

File tree

2 files changed

+39
-4
lines changed

2 files changed

+39
-4
lines changed

README.md

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -871,6 +871,25 @@ class C(A, B): pass
871871
[<class 'C'>, <class 'A'>, <class 'B'>, <class 'object'>]
872872
```
873873

874+
### Property
875+
```python
876+
class MyClass:
877+
@property
878+
def a(self):
879+
return self._a
880+
881+
@a.setter
882+
def a(self, value):
883+
self._a = value
884+
```
885+
886+
```python
887+
>>> el = MyClass()
888+
>>> el.a = 123
889+
>>> el.a
890+
123
891+
```
892+
874893
### Dataclass
875894
**Decorator that automatically generates init(), repr() and eq() special methods.**
876895
```python
@@ -886,6 +905,7 @@ class <class_name>:
886905
* **Function field() is needed because `'<attr_name>: list = []'` would make a list that is shared among all instances.**
887906
* **Default_factory can be any callable.**
888907

908+
889909
### Copy
890910
```python
891911
from copy import copy, deepcopy
@@ -921,10 +941,10 @@ class MyComparable:
921941
```python
922942
class MyHashable:
923943
def __init__(self, a):
924-
self.__a = copy.deepcopy(a)
944+
self._a = copy.deepcopy(a)
925945
@property
926946
def a(self):
927-
return self.__a
947+
return self._a
928948
def __eq__(self, other):
929949
if isinstance(other, type(self)):
930950
return self.a == other.a

index.html

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -823,6 +823,21 @@ <h3 id="multipleinheritance">Multiple Inheritance</h3>
823823
<pre><code class="python language-python hljs"><span class="hljs-meta">&gt;&gt;&gt; </span>C.mro()
824824
[&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;]
825825
</span></code></pre>
826+
<h3 id="property">Property</h3>
827+
<pre><code class="python language-python hljs"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MyClass</span>:</span>
828+
<span class="hljs-meta"> @property</span>
829+
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">a</span><span class="hljs-params">(self)</span>:</span>
830+
<span class="hljs-keyword">return</span> self._a
831+
832+
<span class="hljs-meta"> @a.setter</span>
833+
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">a</span><span class="hljs-params">(self, value)</span>:</span>
834+
self._a = value
835+
</code></pre>
836+
<pre><code class="python language-python hljs"><span class="hljs-meta">&gt;&gt;&gt; </span>el = MyClass()
837+
<span class="hljs-meta">&gt;&gt;&gt; </span>el.a = <span class="hljs-number">123</span>
838+
<span class="hljs-meta">&gt;&gt;&gt; </span>el.a
839+
<span class="hljs-number">123</span>
840+
</code></pre>
826841
<h3 id="dataclass">Dataclass</h3>
827842
<p><strong>Decorator that automatically generates init(), repr() and eq() special methods.</strong></p>
828843
<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 @@ <h3 id="hashable">Hashable</h3>
867882
</ul>
868883
<pre><code class="python language-python hljs"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MyHashable</span>:</span>
869884
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self, a)</span>:</span>
870-
self.__a = copy.deepcopy(a)
885+
self._a = copy.deepcopy(a)
871886
<span class="hljs-meta"> @property</span>
872887
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">a</span><span class="hljs-params">(self)</span>:</span>
873-
<span class="hljs-keyword">return</span> self.__a
888+
<span class="hljs-keyword">return</span> self._a
874889
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__eq__</span><span class="hljs-params">(self, other)</span>:</span>
875890
<span class="hljs-keyword">if</span> isinstance(other, type(self)):
876891
<span class="hljs-keyword">return</span> self.a == other.a

0 commit comments

Comments
 (0)