Skip to content

Commit 42cd25c

Browse files
committed
Correct __slots__ chapter
The text confuses classes and objects. But more importantly, the code doesn't actually compile, since `class` is a keyword.
1 parent c83d39d commit 42cd25c

1 file changed

Lines changed: 7 additions & 7 deletions

File tree

__slots__magic.rst

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ In Python every class can have instance attributes. By default Python
55
uses a dict to store an object’s instance attributes. This is really
66
helpful as it allows setting arbitrary new attributes at runtime.
77

8-
However, in small classes with known attributes it might be a
8+
However, for small classes with known attributes it might be a
99
bottleneck. The ``dict`` wastes a lot of RAM. Python can’t just allocate
1010
a static amount of memory at object creation to store all the
1111
attributes. Therefore it sucks a lot of RAM if you create a lot of
12-
classes (I am talking in thousands and millions). Still there is a way
12+
objects (I am talking in thousands and millions). Still there is a way
1313
to circumvent this issue. It involves the useage of ``__slots__`` to
1414
tell Python not to use a dict, and only allocate space for a fixed set
1515
of attributes. Here is an example with and without ``__slots__``:
@@ -19,9 +19,9 @@ of attributes. Here is an example with and without ``__slots__``:
1919
.. code:: python
2020
2121
class MyClass(object):
22-
def __init__(name, class):
22+
def __init__(name, identifier):
2323
self.name = name
24-
self.class = class
24+
self.identifier = identifier
2525
self.set_up()
2626
# ...
2727
@@ -30,10 +30,10 @@ of attributes. Here is an example with and without ``__slots__``:
3030
.. code:: python
3131
3232
class MyClass(object):
33-
__slots__ = ['name', 'class']
34-
def __init__(name, class):
33+
__slots__ = ['name', 'identifier']
34+
def __init__(name, identifier):
3535
self.name = name
36-
self.class = class
36+
self.identifier = identifier
3737
self.set_up()
3838
# ...
3939

0 commit comments

Comments
 (0)