{"id":2626,"date":"2021-10-24T08:48:57","date_gmt":"2021-10-24T08:48:57","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=2626"},"modified":"2025-03-31T10:13:20","modified_gmt":"2025-03-31T10:13:20","slug":"python-__slots__","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/python-oop\/python-__slots__\/","title":{"rendered":"Python __slots__"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you will learn about the Python <code>__slots__<\/code> and how how to use it to make your class more efficient.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-the-python-__slots__'>Introduction to the Python __slots__ <a href=\"#introduction-to-the-python-__slots__\" class=\"anchor\" id=\"introduction-to-the-python-__slots__\" title=\"Anchor for Introduction to the Python __slots__\">#<\/a><\/h2>\n\n\n\n<p>The following defines a <code>Point2D<\/code> class that has two attributes including <code>x<\/code> and <code>y<\/code> coordinates:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Point2D<\/span>:<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__init__<\/span><span class=\"hljs-params\">(self, x, y)<\/span>:<\/span>\n        self.x = x\n        self.y = y\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__repr__<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-string\">f'Point2D(<span class=\"hljs-subst\">{self.x}<\/span>,<span class=\"hljs-subst\">{self.y}<\/span>)'<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Each instance of the <code>Point2D<\/code> class has its own <code>__dict__<\/code> attribute that stores the <a href=\"https:\/\/www.pythontutorial.net\/python-oop\/python-instance-variables\/\">instance attributes<\/a>. For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">point = Point2D(<span class=\"hljs-number\">0<\/span>, <span class=\"hljs-number\">0<\/span>)\nprint(point.__dict__)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>By default, Python uses the <a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-dictionary\/\">dictionaries<\/a> to 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 <code>Point2D<\/code> class 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 <code>Point2D<\/code> class has only two instance attributes, you can specify the attributes in the slots like this:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Point2D<\/span>:<\/span>\n    __slots__ = (<span class=\"hljs-string\">'x'<\/span>, <span class=\"hljs-string\">'y'<\/span>)\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__init__<\/span><span class=\"hljs-params\">(self, x, y)<\/span>:<\/span>\n        self.x = x\n        self.y = y\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__repr__<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-string\">f'Point2D(<span class=\"hljs-subst\">{self.x}<\/span>,<span class=\"hljs-subst\">{self.y}<\/span>)'<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this example, you assign an <a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-iterables\/\">iterable<\/a> (a <a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-tuples\/\">tuple<\/a>) that contains the attribute names that you&#8217;ll use in the class.<\/p>\n\n\n\n<p>By doing this, Python will not use the <code>__dict__<\/code> for the instances of the class. The following will cause an <code>AttributeError<\/code> error:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">point = Point2D(<span class=\"hljs-number\">0<\/span>, <span class=\"hljs-number\">0<\/span>)\nprint(point.__dict__)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Error:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">AttributeError: <span class=\"hljs-string\">'Point2D'<\/span> object has no attribute __dict__<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Instead, you&#8217;ll see the <code>__slots__<\/code> in the instance of the class. For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">point = Point2D(<span class=\"hljs-number\">0<\/span>, <span class=\"hljs-number\">0<\/span>)\nprint(point.__slots__)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">(<span class=\"hljs-string\">'x'<\/span>, <span class=\"hljs-string\">'y'<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Also, you cannot add more attributes to the instance dynamically at runtime. The following will result in an error:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">point.z = <span class=\"hljs-number\">0<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Error:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">AttributeError: <span class=\"hljs-string\">'Point2D'<\/span> object has no attribute <span class=\"hljs-string\">'z'<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>However, you can add the <a href=\"https:\/\/www.pythontutorial.net\/python-oop\/python-class-attributes\/\">class attributes<\/a> to the class:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">Point2D.color = <span class=\"hljs-string\">'black'<\/span>\npprint(Point2D.__dict__)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">mappingproxy({<span class=\"hljs-string\">'__doc__'<\/span>: <span class=\"hljs-literal\">None<\/span>,\n              <span class=\"hljs-string\">'__init__'<\/span>: &lt;function Point2D.__init__ at <span class=\"hljs-number\">0x000001BBBA841310<\/span>&gt;,\n              <span class=\"hljs-string\">'__module__'<\/span>: <span class=\"hljs-string\">'__main__'<\/span>,\n              <span class=\"hljs-string\">'__repr__'<\/span>: &lt;function Point2D.__repr__ at <span class=\"hljs-number\">0x000001BBBA8413A0<\/span>&gt;,\n              <span class=\"hljs-string\">'__slots__'<\/span>: (<span class=\"hljs-string\">'x'<\/span>, <span class=\"hljs-string\">'y'<\/span>),\n              <span class=\"hljs-string\">'color'<\/span>: <span class=\"hljs-string\">'black'<\/span>,\n              <span class=\"hljs-string\">'x'<\/span>: &lt;member <span class=\"hljs-string\">'x'<\/span> of <span class=\"hljs-string\">'Point2D'<\/span> objects&gt;,\n              <span class=\"hljs-string\">'y'<\/span>: &lt;member <span class=\"hljs-string\">'y'<\/span> of <span class=\"hljs-string\">'Point2D'<\/span> objects&gt;})<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\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\" id='python-__slots__-and-single-inheritance'>Python __slots__ and single inheritance <a href=\"#python-__slots__-and-single-inheritance\" class=\"anchor\" id=\"python-__slots__-and-single-inheritance\" title=\"Anchor for Python __slots__ and single inheritance\">#<\/a><\/h2>\n\n\n\n<p>Let&#8217;s examine the slots in the context of inheritance.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id='the-base-class-uses-the-slots-but-the-subclass-doesnt'>The base class uses the slots but the subclass doesn&#8217;t <a href=\"#the-base-class-uses-the-slots-but-the-subclass-doesnt\" class=\"anchor\" id=\"the-base-class-uses-the-slots-but-the-subclass-doesnt\" title=\"Anchor for The base class uses the slots but the subclass doesn&#039;t\">#<\/a><\/h3>\n\n\n\n<p>The following defines the <code>Point2D<\/code> as the base class and <code>Point3D<\/code> as a subclass that inherits from the <code>Point2D<\/code> class:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Point2D<\/span>:<\/span>\n    __slots__ = (<span class=\"hljs-string\">'x'<\/span>, <span class=\"hljs-string\">'y'<\/span>)\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__init__<\/span><span class=\"hljs-params\">(self, x, y)<\/span>:<\/span>\n        self.x = x\n        self.y = y\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__repr__<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-string\">f'Point2D(<span class=\"hljs-subst\">{self.x}<\/span>,<span class=\"hljs-subst\">{self.y}<\/span>)'<\/span>\n\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Point3D<\/span><span class=\"hljs-params\">(Point2D)<\/span>:<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__init__<\/span><span class=\"hljs-params\">(self, x, y, z)<\/span>:<\/span>\n        super().__init__(x, y)\n        self.z = z\n\n\n<span class=\"hljs-keyword\">if<\/span> __name__ == <span class=\"hljs-string\">'__main__'<\/span>:\n    point = Point3D(<span class=\"hljs-number\">10<\/span>, <span class=\"hljs-number\">20<\/span>, <span class=\"hljs-number\">30<\/span>)\n    print(point.__dict__)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pythontutorial.net\/playground\/?q=Y2xhc3MgUG9pbnQyRDoKICAgIF9fc2xvdHNfXyA9ICgneCcsICd5JykKCiAgICBkZWYgX19pbml0X18oc2VsZiwgeCwgeSk6CiAgICAgICAgc2VsZi54ID0geAogICAgICAgIHNlbGYueSA9IHkKCiAgICBkZWYgX19yZXByX18oc2VsZik6CiAgICAgICAgcmV0dXJuIGYnUG9pbnQyRCh7c2VsZi54fSx7c2VsZi55fSknCgoKY2xhc3MgUG9pbnQzRChQb2ludDJEKToKICAgIGRlZiBfX2luaXRfXyhzZWxmLCB4LCB5LCB6KToKICAgICAgICBzdXBlcigpLl9faW5pdF9fKHgsIHkpCiAgICAgICAgc2VsZi56ID0gegoKCmlmIF9fbmFtZV9fID09ICdfX21haW5fXyc6CiAgICBwb2ludCA9IFBvaW50M0QoMTAsIDIwLCAzMCkKICAgIHByaW50KHBvaW50Ll9fZGljdF9fKQ%3D%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-13\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">{<span class=\"hljs-string\">'z'<\/span>: <span class=\"hljs-number\">30<\/span>}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-13\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>Point3D<\/code> class doesn&#8217;t have slots so its instance has the <code>__dict__<\/code> attribute. In this case, the subclass <code>Point3D<\/code> uses slots from its base class (if available) and uses an instance dictionary.<\/p>\n\n\n\n<p>If you want the <code>Point3D<\/code> class to use slots, you can define additional attributes like this:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-14\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Point3D<\/span><span class=\"hljs-params\">(Point2D)<\/span>:<\/span>\n    __slots__ = (<span class=\"hljs-string\">'z'<\/span>,)\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__init__<\/span><span class=\"hljs-params\">(self, x, y, z)<\/span>:<\/span>\n        super().__init__(x, y)\n        self.z = z<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-14\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Note that you don&#8217;t specify the attributes that are already specified in the <code>__slots__<\/code> of the base class.<\/p>\n\n\n\n<p>Now, the <code>Point3D<\/code> class will use slots for all attributes including x, y, and z.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id='the-base-class-doesnt-use-__slots__-and-the-subclass-doesnt'>The base class doesn&#8217;t use __slots__ and the subclass doesn&#8217;t <a href=\"#the-base-class-doesnt-use-__slots__-and-the-subclass-doesnt\" class=\"anchor\" id=\"the-base-class-doesnt-use-__slots__-and-the-subclass-doesnt\" title=\"Anchor for The base class doesn&#039;t use __slots__ and the subclass doesn&#039;t\">#<\/a><\/h3>\n\n\n\n<p>The following example defines a base class that doesn&#8217;t use the <code>__slots__<\/code> and the subclass does:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-15\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Shape<\/span>:<\/span>\n    <span class=\"hljs-keyword\">pass<\/span>\n\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Point2D<\/span><span class=\"hljs-params\">(Shape)<\/span>:<\/span>\n    __slots__ = (<span class=\"hljs-string\">'x'<\/span>, <span class=\"hljs-string\">'y'<\/span>)\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__init__<\/span><span class=\"hljs-params\">(self, x, y)<\/span>:<\/span>\n        self.x = x\n        self.y = y\n\n\n<span class=\"hljs-keyword\">if<\/span> __name__ == <span class=\"hljs-string\">'__main__'<\/span>:\n    <span class=\"hljs-comment\"># use both slots and dict to store instance attributes<\/span>\n    point = Point2D(<span class=\"hljs-number\">10<\/span>, <span class=\"hljs-number\">10<\/span>)\n    print(point.__slots__)\n    print(point.__dict__)\n\n    <span class=\"hljs-comment\"># can add the attribute at runtime<\/span>\n    point.color = <span class=\"hljs-string\">'black'<\/span>\n    print(point.__dict__)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-15\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pythontutorial.net\/playground\/?q=Y2xhc3MgU2hhcGU6CiAgICBwYXNzCgoKY2xhc3MgUG9pbnQyRChTaGFwZSk6CiAgICBfX3Nsb3RzX18gPSAoJ3gnLCAneScpCgogICAgZGVmIF9faW5pdF9fKHNlbGYsIHgsIHkpOgogICAgICAgIHNlbGYueCA9IHgKICAgICAgICBzZWxmLnkgPSB5CgoKaWYgX19uYW1lX18gPT0gJ19fbWFpbl9fJzoKICAgICMgdXNlIGJvdGggc2xvdHMgYW5kIGRpY3QgdG8gc3RvcmUgaW5zdGFuY2UgYXR0cmlidXRlcwogICAgcG9pbnQgPSBQb2ludDJEKDEwLCAxMCkKICAgIHByaW50KHBvaW50Ll9fc2xvdHNfXykKICAgIHByaW50KHBvaW50Ll9fZGljdF9fKQoKICAgICMgY2FuIGFkZCB0aGUgYXR0cmlidXRlIGF0IHJ1bnRpbWUKICAgIHBvaW50LmNvbG9yID0gJ2JsYWNrJwogICAgcHJpbnQocG9pbnQuX19kaWN0X18p\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-16\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">(<span class=\"hljs-string\">'x'<\/span>, <span class=\"hljs-string\">'y'<\/span>)\n{<span class=\"hljs-string\">'color'<\/span>: <span class=\"hljs-string\">'black'<\/span>}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-16\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this case, the instances of the <code>Point2D<\/code> class uses both <code>__slots__<\/code> and dictionary to store the instance attributes.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Python uses dictionaries to store instance attributes of instances of a class. This allows you to dynamically add more attributes to instances at runtime but also create a memory overhead.<\/li>\n\n\n\n<li>Define <code>__slots__<\/code> in the class if it has predetermined instances attributes to instruct Python not to use dictionaries to store instance attributes. The <code>__slots__<\/code> optimizes the memory if the class has many objects.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id='quiz'>Quiz <a href=\"#quiz\" class=\"anchor\" id=\"quiz\" title=\"Anchor for Quiz\">#<\/a><\/h2>\n\n\n\n<iframe loading=\"lazy\"\n  name=\"quiz\"\n  src=\"\/quiz\/?quiz=__slot__\"\n  height=\"700\"\n  width=\"600\"\n  class=\"iframe\"\n><\/iframe>\n\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful ?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"2626\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-oop\/python-__slots__\/\"\n\t\t\t\tdata-post-title=\"Python __slots__\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"2626\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-oop\/python-__slots__\/\"\n\t\t\t\tdata-post-title=\"Python __slots__\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\t\t\t<input type=\"button\" name=\"wth-submit\" class=\"wth-btn wth-btn-submit\" id=\"wth-submit\" \/>\n\t\t\t<input type=\"button\" class=\"wth-btn wth-btn-cancel\" value=\"Cancel\" \/>\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you will learn about the Python __slots__ and how how to use it to make your class more efficient.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":417,"menu_order":24,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-2626","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/2626","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/comments?post=2626"}],"version-history":[{"count":2,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/2626\/revisions"}],"predecessor-version":[{"id":7316,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/2626\/revisions\/7316"}],"up":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/417"}],"wp:attachment":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/media?parent=2626"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}