{"id":178,"date":"2024-01-25T14:53:16","date_gmt":"2024-01-25T14:53:16","guid":{"rendered":"https:\/\/learnpython.elegantwallp.com\/?p=178"},"modified":"2024-01-25T14:53:17","modified_gmt":"2024-01-25T14:53:17","slug":"python-__slots__","status":"publish","type":"post","link":"https:\/\/learnpython.elegantwallp.com\/2024\/01\/25\/python-__slots__\/","title":{"rendered":"Python __slots__"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you will learn about the Python&nbsp;<code>__slots__<\/code>&nbsp;and how how to use it to make your class more efficient.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction to the Python __slots__<\/h2>\n\n\n\n<p>The following defines a\u00a0<code>Point2D<\/code>\u00a0class that has two attributes including\u00a0<code>x<\/code>\u00a0and\u00a0<code>y<\/code>\u00a0coordinates:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>class Point2D: def __init__(self, x, y): self.x = x self.y = y def __repr__(self): return f'Point2D({self.x},{self.y})'<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Each instance of the\u00a0<code>Point2D<\/code>\u00a0class has its own\u00a0<code>__dict__<\/code>\u00a0attribute that stores the\u00a0instance attributes. For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>point = Point2D(0, 0) print(point.__dict__)<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>By default, Python uses the\u00a0dictionaries\u00a0to manage the instance attributes. The dictionary allows you to add more attributes to the instance dynamically at runtime. However, it also has a certain memory overhead. If the\u00a0<code>Point2D<\/code>\u00a0class has many objects, there will be a lot of memory overhead.<\/p>\n\n\n\n<p>To avoid the memory overhead, Python introduced the slots. If a class only contains fixed (or predetermined) instance attributes, you can use the slots to instruct Python to use a more compact data structure instead of dictionaries.<\/p>\n\n\n\n<p>For example, if the\u00a0<code>Point2D<\/code>\u00a0class has only two instance attributes, you can specify the attributes in the slots like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>class Point2D: __slots__ = ('x', 'y') def __init__(self, x, y): self.x = x self.y = y def __repr__(self): return f'Point2D({self.x},{self.y})'<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>In this example, you assign an\u00a0iterable\u00a0(a\u00a0tuple) that contains the attribute names that you\u2019ll use in the class.<\/p>\n\n\n\n<p>By doing this, Python will not use the\u00a0<code>__dict__<\/code>\u00a0for the instances of the class. The following will cause an\u00a0<code>AttributeError<\/code>\u00a0error:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>point = Point2D(0, 0) print(point.__dict__)<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Error:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>AttributeError: 'Point2D' object has no attribute __dict__<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Instead, you\u2019ll see the\u00a0<code>__slots__<\/code>\u00a0in the instance of the class. For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>point = Point2D(0, 0) print(point.__slots__)<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>('x', 'y')<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Also, you cannot add more attributes to the instance dynamically at runtime. The following will result in an error:<code>point.z = 0<\/code><small>Code language: Python (python)<\/small><\/p>\n\n\n\n<p>Error:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>AttributeError: 'Point2D' object has no attribute 'z'<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>However, you can add the\u00a0class attributes\u00a0to the class:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>Point2D.color = 'black' pprint(Point2D.__dict__)<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>mappingproxy({'__doc__': None, '__init__': &lt;function Point2D.__init__ at 0x000001BBBA841310>, '__module__': '__main__', '__repr__': &lt;function Point2D.__repr__ at 0x000001BBBA8413A0>, '__slots__': ('x', 'y'), 'color': 'black', 'x': &lt;member 'x' of 'Point2D' objects>, 'y': &lt;member 'y' of 'Point2D' objects>})<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>This code works because Python applies the slots to the instances of the class, not the class.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Python __slots__ and single inheritance<\/h2>\n\n\n\n<p>Let\u2019s examine the slots in the context of inheritance.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">The base class uses the slots but the subclass doesn\u2019t<\/h3>\n\n\n\n<p>The following defines the\u00a0<code>Point2D<\/code>\u00a0as the base class and\u00a0<code>Point3D<\/code>\u00a0as a subclass that inherits from the\u00a0<code>Point2D<\/code>\u00a0class:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>class Point2D: __slots__ = ('x', 'y') def __init__(self, x, y): self.x = x self.y = y def __repr__(self): return f'Point2D({self.x},{self.y})' class Point3D(Point2D): def __init__(self, x, y, z): super().__init__(x, y) self.z = z if __name__ == '__main__': point = Point3D(10, 20, 30) print(point.__dict__)<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>{'z': 30}<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>The&nbsp;<code>Point3D<\/code>&nbsp;class doesn\u2019t have slots so its instance has the&nbsp;<code>__dict__<\/code>&nbsp;attribute. In this case, the subclass&nbsp;<code>Point3D<\/code>&nbsp;uses slots from its base class (if available) and uses an instance dictionary.<\/p>\n\n\n\n<p>If you want the\u00a0<code>Point3D<\/code>\u00a0class to use slots, you can define additional attributes like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>class Point3D(Point2D): __slots__ = ('z',) def __init__(self, x, y, z): super().__init__(x, y) self.z = z<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Note that you don\u2019t specify the attributes that are already specified in the&nbsp;<code>__slots__<\/code>&nbsp;of the base class.<\/p>\n\n\n\n<p>Now, the&nbsp;<code>Point3D<\/code>&nbsp;class will use slots for all attributes including x, y, and z.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">The base class doesn\u2019t use __slots__ and the subclass doesn\u2019t<\/h3>\n\n\n\n<p>The following example defines a base class that doesn\u2019t use the\u00a0<code>__slots__<\/code>\u00a0and the subclass does:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>class Shape: pass class Point2D(Shape): __slots__ = ('x', 'y') def __init__(self, x, y): self.x = x self.y = y if __name__ == '__main__': <em># use both slots and dict to store instance attributes<\/em> point = Point2D(10, 10) print(point.__slots__) print(point.__dict__) <em># can add the attribute at runtime<\/em> point.color = 'black' print(point.__dict__)<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>('x', 'y') {'color': 'black'}<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>In this case, the instances of the&nbsp;<code>Point2D<\/code>&nbsp;class uses both&nbsp;<code>__slots__<\/code>&nbsp;and dictionary to store the instance attributes.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: in this tutorial, you will learn about the Python&nbsp;__slots__&nbsp;and how how to use it to make your class more efficient. Introduction to the Python __slots__ The following defines a\u00a0Point2D\u00a0class that has two attributes including\u00a0x\u00a0and\u00a0y\u00a0coordinates: Each instance of the\u00a0Point2D\u00a0class has its own\u00a0__dict__\u00a0attribute that stores the\u00a0instance attributes. For example: By default, Python uses the\u00a0dictionaries\u00a0to manage the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[28],"tags":[],"class_list":["post-178","post","type-post","status-publish","format-standard","hentry","category-4-single-inheritance"],"_links":{"self":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/178","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/comments?post=178"}],"version-history":[{"count":1,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/178\/revisions"}],"predecessor-version":[{"id":179,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/178\/revisions\/179"}],"wp:attachment":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/media?parent=178"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/categories?post=178"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/tags?post=178"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}