<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: waltertaya</title>
    <description>The latest articles on Forem by waltertaya (@waltertaya).</description>
    <link>https://forem.com/waltertaya</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1115153%2F4929fa96-4312-42a9-a2bf-a86cf0c24506.jpeg</url>
      <title>Forem: waltertaya</title>
      <link>https://forem.com/waltertaya</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/waltertaya"/>
    <language>en</language>
    <item>
      <title>Simple way to master inheritance in Python</title>
      <dc:creator>waltertaya</dc:creator>
      <pubDate>Sat, 16 Dec 2023 11:13:56 +0000</pubDate>
      <link>https://forem.com/waltertaya/simple-way-to-master-inheritance-in-python-2eg9</link>
      <guid>https://forem.com/waltertaya/simple-way-to-master-inheritance-in-python-2eg9</guid>
      <description>&lt;p&gt;Inheritance is mechanism that allows a class to inherit properties and attributes from another class.&lt;br&gt;
General Syntax of inheritance&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class DerivedClassName(BaseClassName):
    &amp;lt;statement-1&amp;gt;
    .
    .
    .
    &amp;lt;statement-N&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The name BaseClassName(super class) must be defined in a namespace accessible from the scope containing the derived class definition.&lt;br&gt;
If the super class is defined in another module then the syntax is: &lt;br&gt;
&lt;code&gt;class DerivedClassName(moduleName.BaseClassName):&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;How inheritance work?&lt;br&gt;
Execution of a derived class definition proceeds the same as for a base class. When the class object is constructed, the base class is remembered. This is used for resolving attribute references: if a requested attribute is not found in the class, the search proceeds to look in the base class. This rule is applied recursively if the base class itself is derived from some other class.&lt;/p&gt;
&lt;h2&gt;
  
  
  Method Overriding
&lt;/h2&gt;

&lt;p&gt;Derived classes may override methods of their base classes. Because methods have no special privileges when calling other methods of the same object, a method of a base class that calls another method defined in the same base class may end up calling a method of a derived class that overrides it. (For C++ programmers: all methods in Python are effectively virtual.)&lt;/p&gt;

&lt;p&gt;An overriding method in a derived class may in fact want to extend rather than simply replace the base class method of the same name. There is a simple way to call the base class method directly: just call&lt;br&gt;
&lt;br&gt;
 &lt;code&gt;BaseClassName.methodname(self, arguments)&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;This is occasionally useful to clients as well. (Note that this only works if the base class is accessible as BaseClassName in the global scope.)&lt;/p&gt;

&lt;h2&gt;
  
  
  Built Functions that works with inheritance
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;a href="https://www.w3schools.com/python/ref_func_isinstance.asp"&gt;&lt;code&gt;isinstance()&lt;/code&gt;&lt;/a&gt; to check an instance's type:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;isinstance(obj, int)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;will return &lt;code&gt;True&lt;/code&gt; iff &lt;code&gt;obj.__class__&lt;/code&gt; is int or some class derived from &lt;a href="//www.google.com"&gt;int&lt;/a&gt;.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;a href="https://www.w3schools.com/python/ref_func_issubclass.asp#:~:text=The%20issubclass()%20function%20returns,the%20specified%20object%2C%20otherwise%20False%20."&gt;&lt;code&gt;issubclass()&lt;/code&gt;&lt;/a&gt;
to check class inheritance:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;issubclass(bool, int)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;will return &lt;code&gt;True&lt;/code&gt; since bool is a subclass of int.&lt;/p&gt;

&lt;h2&gt;
  
  
  Multiple Inheritance
&lt;/h2&gt;

&lt;p&gt;Python supports a form of multiple inheritance as well.&lt;br&gt;
Syntax&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class DerivedClassName(Base1, Base2, Base3):
    &amp;lt;statement-1&amp;gt;
    .
    .
    .
    &amp;lt;statement-N&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>beginners</category>
      <category>tutorial</category>
      <category>python</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
