-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathSlotsInOldStyleClass.qhelp
37 lines (28 loc) · 1.5 KB
/
SlotsInOldStyleClass.qhelp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<!DOCTYPE qhelp PUBLIC
"-//Semmle//qhelp//EN"
"qhelp.dtd">
<qhelp>
<overview>
<p>The ability to override the class dictionary using a <code>__slots__</code> declaration
is supported only by new-style classes. When you add a <code>__slots__</code> declaration to an
old-style class it just creates a class attribute called <code>__slots__</code>.</p>
</overview>
<recommendation>
<p>If you want to override the dictionary for a class, then ensure that the class is a new-style class.
You can convert an old-style class to a new-style class by inheriting from <code>object</code>.</p>
</recommendation>
<example>
<p>In the following Python 2 example the <code>Point</code> class is an old-style class (no inheritance). The
<code>__slots__</code> declaration in this class creates a class attribute called <code>__slots__</code>, the class
dictionary is unaffected. The <code>Point2</code> class is a new-style class so the
<code>__slots__</code> declaration causes special compact attributes to be created for each name in
the slots list and saves space by not creating attribute dictionaries.</p>
<sample src="SlotsInOldStyleClass.py" />
</example>
<references>
<li>Python Glossary: <a href="http://docs.python.org/glossary.html#term-new-style-class">New-style class</a>.</li>
<li>Python Language Reference: <a href="http://docs.python.org/2/reference/datamodel.html#newstyle">New-style and classic
classes</a>,
<a href="http://docs.python.org/reference/datamodel.html#__slots__">__slots__</a>.</li>
</references>
</qhelp>