Skip to content

Commit 5ca3abd

Browse files
author
Saeid Darvish
committed
l21: complete __slots__
1 parent ddc5483 commit 5ca3abd

1 file changed

Lines changed: 193 additions & 0 deletions

File tree

lessons/l21.rst

Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,199 @@ __slots__
9393
از مزایای ``__slots__`` می‌توان به کاهش مصرف حافطه (RAM) به خصوص در مورد کلاس‌هایی که قرار است اشیایی خیلی زیادی از آن‌ها ایجاد گردد، اشاره نمود.
9494

9595

96+
از طریق ``__slots__`` همچنین می‌توان اجازه داد که کدام Attribute در آینده برای شی ایجاد گردد:
97+
98+
.. code-block:: python
99+
:linenos:
100+
101+
class Sample:
102+
103+
__slots__ = ('a', 'b', 'c')
104+
105+
def __init__(self, a, b):
106+
self.a = a
107+
self.b = b
108+
109+
objet = Sample(1, 2)
110+
111+
objet.c = 3
112+
113+
print('a: ', objet.a)
114+
print('b: ', objet.b)
115+
print('c: ', objet.c)
116+
117+
objet.d = 4
118+
119+
::
120+
121+
122+
a: 1
123+
b: 2
124+
c: 3
125+
Traceback (most recent call last):
126+
File "sample.py", line 17, in <module>
127+
objet.d = 4
128+
AttributeError: 'Sample' object has no attribute 'd'
129+
130+
131+
132+
**اکنون نمونه کد زیر را در وضعیت وراثت در نظر بگیرید:**
133+
134+
.. code-block:: python
135+
:linenos:
136+
137+
class Parent:
138+
def __init__(self, a, b):
139+
self.a = a
140+
self.b = b
141+
142+
143+
class Child(Parent):
144+
def __init__(self, a, b):
145+
super().__init__(a, b)
146+
147+
148+
child = Child(1, 2)
149+
print(child.__dict__)
150+
151+
child.c = 3
152+
print(child.__dict__)
153+
154+
print('a: ', child.a)
155+
print('b: ', child.b)
156+
print('c: ', child.c)
157+
158+
::
159+
160+
{'a': 1, 'b': 2}
161+
{'a': 1, 'b': 2, 'c': 3}
162+
a: 1
163+
b: 2
164+
c: 3
165+
166+
167+
اگر کلاس Parent شامل ``__slots__`` باشد و در نتیجه فاقد ``__dict__``:
168+
169+
.. code-block:: python
170+
:linenos:
171+
172+
class Parent:
173+
__slots__ = ('a', 'b')
174+
175+
def __init__(self, a, b):
176+
self.a = a
177+
self.b = b
178+
179+
180+
class Child(Parent):
181+
182+
def __init__(self, a, b):
183+
super().__init__(a, b)
184+
185+
186+
child = Child(1, 2)
187+
print(child.__dict__)
188+
189+
child.c = 3
190+
print(child.__dict__)
191+
192+
print('a: ', child.a)
193+
print('b: ', child.b)
194+
print('c: ', child.c)
195+
196+
197+
::
198+
199+
{}
200+
{'c': 3}
201+
a: 1
202+
b: 2
203+
c: 3
204+
205+
اگر هر دو کلاس شامل ``__slots__`` باشند:
206+
207+
.. code-block:: python
208+
:linenos:
209+
210+
class Parent:
211+
__slots__ = ('a', 'b')
212+
213+
def __init__(self, a, b):
214+
self.a = a
215+
self.b = b
216+
217+
218+
class Child(Parent):
219+
__slots__ = ('c')
220+
221+
def __init__(self, a, b):
222+
super().__init__(a, b)
223+
224+
225+
child = Child(1, 2)
226+
227+
child.c = 3
228+
print('a: ', child.a)
229+
print('b: ', child.b)
230+
print('c: ', child.c)
231+
232+
::
233+
234+
a: 1
235+
b: 2
236+
c: 3
237+
238+
239+
**در وراثت چندگانه،** چنانچه ``__slots__`` مربوط به superclassها حاوی مقدار تکراری باشد، آنگاه باعث بروز خطا می‌گردد:
240+
241+
.. code-block:: python
242+
:linenos:
243+
244+
class ParentOne:
245+
__slots__ = ('a', 'b')
246+
247+
class ParentTwo:
248+
__slots__ = ('z', 'b')
249+
250+
251+
class Child(ParentOne, ParentTwo):
252+
__slots__ = ('c')
253+
254+
255+
child = Child()
256+
257+
::
258+
259+
Traceback (most recent call last):
260+
File "sample.py", line 8, in <module>
261+
class Child(ParentOne, ParentTwo):
262+
TypeError: multiple bases have instance lay-out conflict
263+
264+
265+
بهتر است superclassها حاوی یک ``__slots__`` خالی (شی تاپل خالی) باشند و هر subclass خود محتوای ``__slots__`` خود را تعریف نماید:
266+
267+
.. code-block:: python
268+
:linenos:
269+
270+
class ParentOne:
271+
__slots__ = ()
272+
273+
class ParentTwo:
274+
__slots__ = ()
275+
276+
277+
class Child(ParentOne, ParentTwo):
278+
__slots__ = ('a', 'b', 'z', 'c')
279+
280+
281+
child = Child()
282+
283+
284+
|
285+
286+
287+
[`مطلب مرتبط در StackOverflow <https://stackoverflow.com/a/28059785>`__]
288+
96289
Decorators
97290
----------------------------
98291

0 commit comments

Comments
 (0)