Skip to content

Commit

Permalink
Merge pull request #1 from phihag/patch-1
Browse files Browse the repository at this point in the history
Correct __slots__ chapter
  • Loading branch information
yasoob committed Aug 17, 2015
2 parents c83d39d + 42cd25c commit 44c6dd4
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions __slots__magic.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ In Python every class can have instance attributes. By default Python
uses a dict to store an object’s instance attributes. This is really
helpful as it allows setting arbitrary new attributes at runtime.

However, in small classes with known attributes it might be a
However, for small classes with known attributes it might be a
bottleneck. The ``dict`` wastes a lot of RAM. Python can’t just allocate
a static amount of memory at object creation to store all the
attributes. Therefore it sucks a lot of RAM if you create a lot of
classes (I am talking in thousands and millions). Still there is a way
objects (I am talking in thousands and millions). Still there is a way
to circumvent this issue. It involves the useage of ``__slots__`` to
tell Python not to use a dict, and only allocate space for a fixed set
of attributes. Here is an example with and without ``__slots__``:
Expand All @@ -19,9 +19,9 @@ of attributes. Here is an example with and without ``__slots__``:
.. code:: python
class MyClass(object):
def __init__(name, class):
def __init__(name, identifier):
self.name = name
self.class = class
self.identifier = identifier
self.set_up()
# ...
Expand All @@ -30,10 +30,10 @@ of attributes. Here is an example with and without ``__slots__``:
.. code:: python
class MyClass(object):
__slots__ = ['name', 'class']
def __init__(name, class):
__slots__ = ['name', 'identifier']
def __init__(name, identifier):
self.name = name
self.class = class
self.identifier = identifier
self.set_up()
# ...
Expand Down

0 comments on commit 44c6dd4

Please sign in to comment.