diff --git a/README.md b/README.md index b2dd3bb..bfea0f8 100644 --- a/README.md +++ b/README.md @@ -871,6 +871,25 @@ class C(A, B): pass [, , , ] ``` +### 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 **Decorator that automatically generates init(), repr() and eq() special methods.** ```python @@ -886,6 +905,7 @@ class : * **Function field() is needed because `': list = []'` would make a list that is shared among all instances.** * **Default_factory can be any callable.** + ### Copy ```python from copy import copy, deepcopy @@ -921,10 +941,10 @@ class MyComparable: ```python class MyHashable: def __init__(self, a): - self.__a = copy.deepcopy(a) + self._a = copy.deepcopy(a) @property def a(self): - return self.__a + return self._a def __eq__(self, other): if isinstance(other, type(self)): return self.a == other.a diff --git a/index.html b/index.html index 06024a1..0d3beaa 100644 --- a/index.html +++ b/index.html @@ -823,6 +823,21 @@ creature = Creature(Point(0, >>> C.mro() [<class 'C'>, <class 'A'>, <class 'B'>, <class 'object'>] +

Property

+
class MyClass:
+    @property
+    def a(self):
+        return self._a
+
+    @a.setter
+    def a(self, value):
+        self._a = value
+
+
>>> el = MyClass()
+>>> el.a = 123
+>>> el.a
+123
+

Dataclass

Decorator that automatically generates init(), repr() and eq() special methods.

from dataclasses import dataclass, field
@@ -867,10 +882,10 @@ creature  = Creature(Point(0, class MyHashable:
     def __init__(self, a):
-        self.__a = copy.deepcopy(a)
+        self._a = copy.deepcopy(a)
     @property
     def a(self):
-        return self.__a
+        return self._a
     def __eq__(self, other):
         if isinstance(other, type(self)):
             return self.a == other.a