<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="../assets/xml/rss.xsl" media="all"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Readul Hasan Chayan [Heemayl] (Posts about oop)</title><link>https://heemayl.net/</link><description></description><atom:link href="https://heemayl.net/categories/oop.xml" rel="self" type="application/rss+xml"></atom:link><language>en</language><copyright>Contents © 2024 &lt;a href="mailto:me@heemayl.net"&gt;Readul Hasan Chayan [Heemayl]&lt;/a&gt; 
&lt;a rel="license" href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank"&gt;
&lt;img alt="Creative Commons License BY-NC-SA"
style="border-width:0; margin-top: 10px; margin-bottom: 5px;"
src="https://i.creativecommons.org/l/by-nc-sa/4.0/88x31.png"&gt;&lt;/a&gt;
</copyright><lastBuildDate>Thu, 07 Mar 2024 19:03:10 GMT</lastBuildDate><generator>Nikola (getnikola.com)</generator><docs>http://blogs.law.harvard.edu/tech/rss</docs><item><title>Python : Can we use magic/dunder methods as instance attributes directly instead of putting them in type definition?</title><link>https://heemayl.net/posts/python-can-we-use-magic-dunder-methods-as-instance-attributes-directly-instead-of-putting-them-in-type-definition/</link><dc:creator>Readul Hasan Chayan [Heemayl]</dc:creator><description>&lt;div&gt;&lt;p&gt;&lt;strong&gt;&lt;em&gt;No, we can't.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The magic/dunder methods are looked up implicitly on the type definition while special functions, keywords, or operators are to be resolved by the runtime. They don't follow the regular attribute lookup procedure i.e. doesn't follow the &lt;code&gt;__getattribute__&lt;/code&gt; &lt;em&gt;chain&lt;/em&gt;. This makes them fast to access as the runtime only needs to search on the type, not on instance &lt;code&gt;dict&lt;/code&gt;.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;So, what about &lt;/em&gt;classes&lt;em&gt; themselves? What dunder methods should be used when any special function is called on them?&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;As any class is an &lt;em&gt;instance&lt;/em&gt; of a &lt;em&gt;metaclass&lt;/em&gt;, the dunder methods defined on the metaclass would be used.&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;Let's see a few examples to get a clear overview of all these. We'll be using the &lt;code&gt;repr&lt;/code&gt; function, for which the interpreter will call &lt;code&gt;__repr__&lt;/code&gt; dunder method implicitly, in order to get the &lt;em&gt;representation&lt;/em&gt; of an object.&lt;/p&gt;
&lt;p&gt;&lt;a href="https://heemayl.net/posts/python-can-we-use-magic-dunder-methods-as-instance-attributes-directly-instead-of-putting-them-in-type-definition/"&gt;Read more…&lt;/a&gt; (1 min remaining to read)&lt;/p&gt;&lt;/div&gt;</description><category>dunder</category><category>metaclass</category><category>oop</category><category>python</category><guid>https://heemayl.net/posts/python-can-we-use-magic-dunder-methods-as-instance-attributes-directly-instead-of-putting-them-in-type-definition/</guid><pubDate>Fri, 22 Mar 2019 13:11:05 GMT</pubDate></item></channel></rss>