Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] Overriding in Python

#1
Overriding in Python

<div><p>In this article, we are going to explore the concept of overriding in Python. We are also going to explore what are magic methods and abstract classes.</p>
<h2>Introduction</h2>
<p>Overriding is an interesting concept in <a href="https://blog.finxter.com/object-oriented-programming-terminology-cheat-sheet/" target="_blank" rel="noreferrer noopener" title="[Python OOP Cheat Sheet] A Simple Overview of Object-Oriented Programming">object-oriented programming</a>.  When the method definitions of a Base Class are changed in a Subclass (Derived) Class this is called a method override.  You are keeping the same signature of the method but changing the definition or implementation of a method defined by one of the ancestors.  There is no special syntax or additional keywords needed to do method overriding in Python.  It is a very important object-oriented programming concept since it makes inheritance exploit its full power.  In essence, you are not duplicating code, thus following the programming principle of DRY (do not repeat yourself), and you can enhance the method in the subclass.</p>
<p>To understand the concept of overrides, you must know the basic concepts of object-oriented programming such as classes and inheritance.  There are many resources on the internet about OOP.  A really good resource is Finxter Academy’s object-oriented Python class: <a href="https://academy.finxter.com/university/object-oriented-python/">https://academy.finxter.com/university/object-oriented-python/</a></p>
<h2>Need for Overrides</h2>
<p>In the following example, you see how inheritance works and the problem of not overriding a method in the subclass.  The Parent class has a method <code>whoami</code> that displays <code>“I am a parent”</code>.  This method is inherited by the <code>Child</code> class.  Calling the <code>whoami </code>method from the Child class, calls the inherited method from the Parent class and thus displays <code>“I am a parent”</code> which is wrong.</p>
<p>Inheritance example with no method overriding:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">class Parent(): def whoami(self): print("I am a parent") class Child(Parent): def play(self): print(" I am playing") child = Child()
child.whoami() # Output:
# I am a parent
</pre>
<h2>Basic Override</h2>
<p>The next example, shows how basic overriding works.  In this example the <code>Child</code> class has a definition of the <code>whoami</code> method that overrides the method from the <code>Parent</code> class. Calling the <code>whoami</code> method from the <code>Child</code> class now displays <code>“I am a child”</code>.</p>
<p>Basic Overriding example:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">#Overriding
class Parent(): def whoami(self): print("I am a parent") class Child(Parent): def play(self): print(" I am playing") def whoami(self): print("I am a child") parent = Parent()
parent.whoami()
print()
child = Child()
child.whoami() # Output:
# I am a parent
# I am a child
</pre>
<h2>Extending a Method Through Overrides</h2>
<p>The third example shows how you can <strong>extend</strong> a method in a Base Class by overriding the method in the Subclass.  To do that we use the <code>super()</code> built-in function.  It returns a proxy object that allows us to access methods of the base class.  We can refer to the base class from the subclass without having to call the base class name explicitly.</p>
<p>The <code>Employee</code> class has the following details for the employee: employee number, employee name, salary and, department number.  This information is passed to the instantiated object in the <code><a href="https://blog.finxter.com/python-init/" target="_blank" rel="noreferrer noopener" title="What is __init__ in Python?">__init__</a></code> method.  The <code>showEmployee</code> method of the class then displays this information formatted using new lines.</p>
<p>The <code>Salesman</code> class inherits from the <code>Employee</code> class.  By default, it inherits the <code>showEmployee</code> method as-is from the <code>Employee</code> class.  The only problem is that we want to display the commission that the salesman receives as part of the printed information.  Here is where overriding is needed.  We want to reuse the <code>showEmployee</code> method from the <code>Employee</code> class but we want to extend it to add the commission information.  This is accomplished by using the <code>super()</code> built-in function.  In the example below you see that in the <code>Salesman</code> class, <code>super()</code> is used twice.  The <code>__init__</code> method uses it to call the <code>__init__</code> method of the Employee base class and the <code>showEmployee</code> uses it to override the <code>showEmployee</code> method of the <code>Employee</code> base class.  In it, we display the employee information for the salesman plus the commission for the salesman.</p>
<p>A third class called <code>CEO</code> uses the same logic as before.  The <code>showEmployee</code> method in this case displays the employee information plus the stock options for the <code>CEO</code>.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">class Employee(): def __init__(self, empno, ename, salary, deptno): self.Empno = empno self.Ename = ename self.Salary = salary self.Deptno = deptno def showEmployee(self): print("Employee # : {}\nEmployee Name : {}\nSalary : {}\nDepartment : {}".format(self.Empno, self.Ename, self.Salary, self.Deptno)) class Salesman(Employee): def __init__(self, empno, ename, salary, deptno, comm): self.Commission = comm super().__init__(empno, ename, salary, deptno) def showEmployee(self): print("Salesman Profile") super().showEmployee() print("Commision : ", self.Commission) class CEO(Employee): def __init__(self, empno, ename, salary, deptno, stock): self.Stock = stock super().__init__(empno, ename, salary, deptno) def showEmployee(self): print("CEO Profile") super().showEmployee() print("Stock Options : ", self.Stock) salesman = Salesman(200, "John Doe", 67000, "Sales", 100)
salesman.showEmployee()
print("")
ceo = CEO(40, "Jennifer Smith", 300000, "Director", 1000000)
ceo.showEmployee() </pre>
<p>Output:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">Salesman Profile
Employee # : 200
Employee Name : John Doe
Salary : 67000
Department : Sales
Commision : 100 CEO Profile
Employee # : 40
Employee Name : Jennifer Smith
Salary : 300000
Department : Director
Stock Options : 1000000
</pre>
<h2>Multiple Inheritance Overriding</h2>
<p>Understanding <strong><em>multiple inheritance</em></strong> has its own challenges.  One of them is the use of <code>super()</code>.  Here is a link to an article about this issue: <a href="https://www.datacamp.com/community/tutorials/super-multiple-inheritance-diamond-problem">https://www.datacamp.com/community/tutorials/super-multiple-inheritance-diamond-problem</a></p>
<p>In the example below, I wanted to show a way to override a method from a subclass with multiple inheritance.  The example consists of three classes: <code>Account</code>, <code>Customer</code>, and <code>Orders</code>.  </p>
<ul>
<li>The <code>Account</code> class has its name and number and a display method which displays this information.  </li>
<li>The <code>Customer</code> class has also its name and number and a display method that displays this information.  </li>
<li>The <code>Orders</code> class displays the orders information for a customer in a specific account.  It inherits from both the <code>Account</code> and Orders class.  The display method in this class overrides the display methods of the base classes.  The display method prints the Account information, the Customer information, and the Orders information</li>
</ul>
<p>Here’s an example:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">class Account(): def __init__(self, name, number): self.Name = name self.Number = number def display(self): print("Account # : {}\nAccount Name : {}".format(self.Number, self.Name)) class Customer(): def __init__(self, name, number): self.Name = name self.Number = number def display(self): print("Customer # : {}\nCustomer Name : {}".format(self.Number, self.Name)) class Orders(Account, Customer): def __init__(self, acctnumber, acctname, custnumber, custname, ordnumber, ordnamename, product, qty): self.OrdNumber = ordnumber self.Product = product self.Qty = qty self.OrdName = ordnamename self.acct = Account(acctname, acctnumber) self.cust = Customer(custname, custnumber) def display(self): print("Order Information") self.acct.display() self.cust.display() print("Order # : {}\nOrder Name : {}\nProduct : {}\nQuantiy : {}".format(self.OrdNumber, self.OrdName, self.Product, self.Qty)) acct = Account("AB Enterprise", 12345)
acct.display()
print("")
cust = Customer("John Smith", 45678)
cust.display()
print("")
order = Orders(12345, "AB Enterprise", 45678,"John Smith", 1, "Order 1", "Widget", 5, )
order.display()
</pre>
<p>Output:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">Account # : 12345
Account Name : AB Enterprise Customer # : 45678
Customer Name : John Smith Order Information
Account # : 12345
Account Name : AB Enterprise
Customer # : 45678
Customer Name : John Smith
Order # : 1
Order Name : Order 1
Product : Widget
Quantiy : 5
</pre>
<h2>Different Overriding Scenarios</h2>
<h3>1 – Class Methods</h3>
<p>Class methods are special in the sense that they can be called on a class by itself or by instances of a class.&nbsp; They bind to a class so this means that the first argument passed to the method is a class rather than an instance.</p>
<p>Class methods are written similarly to regular instance methods.  One difference is the use of the <a href="https://blog.finxter.com/closures-and-decorators-in-python/" target="_blank" rel="noreferrer noopener" title="Closures and Decorators in Python">decorator </a><code>@classmethod</code> to identify that the method is a class method.  Also, by convention, instead of using self to reference the instance of a class, <code>cls</code> is using to reference the class.  For example:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">class Account(): @classmethod def getClassVersion(cls): print("Account class version is 1.0”)
</pre>
<p>For more information about class methods check <a href="https://www.geeksforgeeks.org/classmethod-in-python" target="_blank" rel="noreferrer noopener" title="https://www.geeksforgeeks.org/classmethod-in-python">this </a>website.</p>
</p>
<p>Here is an example of the problem you will encounter when overriding a class method:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">class ParentClass: @classmethod def display(cls, arg): print("ParentClass") class SubClass(ParentClass): @classmethod def display(cls, arg): ret = ParentClass.create(cls, arg) print("Subclass") return ret SubClass.display("test")
</pre>
<p>Output:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">create() takes 2 positional arguments but 3 were given</pre>
<p>In the <code>Subclass</code>, the call to the <code>ParentClass</code> create method is not an unbound call the way it happens with a normal instance method.  The outcome of this call is a <a href="https://blog.finxter.com/how-to-solve-python-typeerror-int-object-is-not-iterable/" target="_blank" rel="noreferrer noopener" title="How to Solve Python “TypeError: ‘int’ object is not iterable”?">TypeError </a>because the method received too many arguments.</p>
<p>The solution is to use <code>super()</code> in order to successfully use the parent implementation.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">class ParentClass: @classmethod def display(cls, arg): print("ParentClass") class SubClass(ParentClass): @classmethod def display(cls, arg): ret = super(SubClass, cls).create(arg) print("Subclass") return ret SubClass.display("test")
</pre>
<p>Output:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">ParentClass
Subclass</pre>
</p>
<h3>2 – Magic Methods</h3>
<p><strong>What are magic methods?</strong></p>
<p>Magic methods are a set of methods that Python automatically associates with every class definition. Your classes can override these magic methods to implement different behaviors and make them act just like Python’s built-in classes. Below you will see examples of two of the most common ones: <code>__str__</code> and <code>__repl__</code>. Using these two methods, you can implement how your objects are displayed as strings, which will be of importance while debugging and presenting the information to the user.  Overriding these methods adds to the flexibility and power of Python.  </p>
<p>The <code>__str__</code> method of a class is used when Python prints an object. This magic method is called by the <code>str</code> built-in function.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">class DemoMagic(): def display(self): print("Demo Magic class") varDemo = DemoMagic()
varDemo.display()
str(varDemo) </pre>
<p>Output:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">Demo Magic class '&lt;__main__.DemoMagic object at 0x000002A7A7F64408>' class DemoMagic():
def display(self): print("Demo Magic class") def __str__(self): return "Override of str function" varDemo = DemoMagic()
varDemo.display()
str(varDemo) </pre>
<p>Output:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">Demo Magic class 'Override of str function'
</pre>
<p>The <code>__repr__</code> method of a class is used to display the details of an object’s values. This magic method is called by the built-in the <code>repr</code> function.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">class Person(object): def __init__(self, firstname, lastname): self.first = firstname self.last = lastname def __repr__(self): return "%s %s" % (self.first, self.last) Tom = Person("Tom", "Sawyer")
repr(Tom)
</pre>
<p>Output</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">'Tom Sawyer'</pre>
<p>Here is a list of magic methods.&nbsp; You can learn more about them here (<a href="https://www.tutorialsteacher.com/python/magic-methods-in-python">https://www.tutorialsteacher.com/python/magic-methods-in-python</a>) and think how to override them to fit your needs:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">'__abs__', '__add__', '__and__', '__bool__', '__ceil__', '__class__', '__delattr__', '__dir__', '__divmod__', '__doc__', '__eq__', '__float__', '__floor__', '__floordiv__', '__format__', '__ge__', '__getattribute__', '__getnewargs__', '__gt__', '__hash__', '__index__', '__init__', '__init_subclass__', '__int__', '__invert__', '__le__', '__lshift__', '__lt__', '__mod__', '__mul__', '__ne__', '__neg__', '__new__', '__or__', '__pos__', '__pow__', '__radd__', '__rand__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__', '__ror__', '__round__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', '__trunc__', '__xor__'</pre>
<h3>3 – Abstract Classes  </h3>
<p>An abstract class is a blueprint that subclasses must follow. &nbsp;It can be viewed as a contract between the abstract class and the subclass.&nbsp; The abstract class tells you what to do by providing an empty abstract method and the subclass must implement it.&nbsp; By default, you must override the abstract methods defined by the abstract class.&nbsp; In other terms, we are providing a common interface for different implementations of a class.&nbsp; This is very useful for large projects where certain functionality must be implemented.</p>
<p>Here is an example of an abstract class and the overrriden methods in the subclasses.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">from abc import ABC, abstractmethod class Animal(ABC): @abstractmethod def whoami(self): pass class Dog(Animal): def move(self): print("I am a dog") class Cat(Animal): def move(self): print("I am a cat") </pre>
<h2>Conclusion</h2>
<p>Overriding is a basic concept used in many places in object-oriented programming.&nbsp; You must understand it to be able to implement better classes and modify their behavior.&nbsp; This article shows you some examples where you can use overrides in Python.&nbsp; Other concepts mentioned that are worth learning more about are: magic method and abstract classes.</p>
<p>The post <a href="https://blog.finxter.com/overriding-in-python/" target="_blank" rel="noopener noreferrer">Overriding in Python</a> first appeared on <a href="https://blog.finxter.com/" target="_blank" rel="noopener noreferrer">Finxter</a>.</p>
</div>


https://www.sickgaming.net/blog/2020/10/...in-python/
Reply



Forum Jump:


Users browsing this thread:
3 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016