Create an account


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

#1
Overriding in Python

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.

Introduction


Overriding is an interesting concept in object-oriented programming.  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.

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: https://academy.finxter.com/university/object-oriented-python/

Need for Overrides


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 whoami that displays “I am a parent”.  This method is inherited by the Child class.  Calling the whoami method from the Child class, calls the inherited method from the Parent class and thus displays “I am a parent” which is wrong.

Inheritance example with no method overriding:

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

Basic Override


The next example, shows how basic overriding works.  In this example the Child class has a definition of the whoami method that overrides the method from the Parent class. Calling the whoami method from the Child class now displays “I am a child”.

Basic Overriding example:

#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

Extending a Method Through Overrides


The third example shows how you can extend a method in a Base Class by overriding the method in the Subclass.  To do that we use the super() 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.

The Employee 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 __init__ method.  The showEmployee method of the class then displays this information formatted using new lines.

The Salesman class inherits from the Employee class.  By default, it inherits the showEmployee method as-is from the Employee 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 showEmployee method from the Employee class but we want to extend it to add the commission information.  This is accomplished by using the super() built-in function.  In the example below you see that in the Salesman class, super() is used twice.  The __init__ method uses it to call the __init__ method of the Employee base class and the showEmployee uses it to override the showEmployee method of the Employee base class.  In it, we display the employee information for the salesman plus the commission for the salesman.

A third class called CEO uses the same logic as before.  The showEmployee method in this case displays the employee information plus the stock options for the CEO.

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()

Output:

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

Multiple Inheritance Overriding


Understanding multiple inheritance has its own challenges.  One of them is the use of super().  Here is a link to an article about this issue: https://www.datacamp.com/community/tutorials/super-multiple-inheritance-diamond-problem

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: Account, Customer, and Orders

  • The Account class has its name and number and a display method which displays this information. 
  • The Customer class has also its name and number and a display method that displays this information. 
  • The Orders class displays the orders information for a customer in a specific account.  It inherits from both the Account 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

Here’s an example:

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()

Output:

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

Different Overriding Scenarios


1 – Class Methods


Class methods are special in the sense that they can be called on a class by itself or by instances of a class.  They bind to a class so this means that the first argument passed to the method is a class rather than an instance.

Class methods are written similarly to regular instance methods.  One difference is the use of the decorator @classmethod to identify that the method is a class method.  Also, by convention, instead of using self to reference the instance of a class, cls is using to reference the class.  For example:

class Account(): @classmethod def getClassVersion(cls): print("Account class version is 1.0”)

For more information about class methods check this website.

Here is an example of the problem you will encounter when overriding a class method:

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")

Output:

create() takes 2 positional arguments but 3 were given

In the Subclass, the call to the ParentClass create method is not an unbound call the way it happens with a normal instance method.  The outcome of this call is a TypeError because the method received too many arguments.

The solution is to use super() in order to successfully use the parent implementation.

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")

Output:

ParentClass
Subclass

2 – Magic Methods


What are magic methods?

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: __str__ and __repl__. 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.  

The __str__ method of a class is used when Python prints an object. This magic method is called by the str built-in function.

class DemoMagic(): def display(self): print("Demo Magic class") varDemo = DemoMagic()
varDemo.display()
str(varDemo)

Output:

Demo Magic class '<__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)

Output:

Demo Magic class 'Override of str function'

The __repr__ 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 repr function.

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)

Output

'Tom Sawyer'

Here is a list of magic methods.  You can learn more about them here (https://www.tutorialsteacher.com/python/magic-methods-in-python) and think how to override them to fit your needs:

'__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__'

3 – Abstract Classes  


An abstract class is a blueprint that subclasses must follow.  It can be viewed as a contract between the abstract class and the subclass.  The abstract class tells you what to do by providing an empty abstract method and the subclass must implement it.  By default, you must override the abstract methods defined by the abstract class.  In other terms, we are providing a common interface for different implementations of a class.  This is very useful for large projects where certain functionality must be implemented.

Here is an example of an abstract class and the overrriden methods in the subclasses.

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") 

Conclusion


Overriding is a basic concept used in many places in object-oriented programming.  You must understand it to be able to implement better classes and modify their behavior.  This article shows you some examples where you can use overrides in Python.  Other concepts mentioned that are worth learning more about are: magic method and abstract classes.

The post Overriding in Python first appeared on Finxter.



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



Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tut] Python abs() Function xSicKxBot 0 5 9 hours ago
Last Post: xSicKxBot
  [Tut] Python Built-In Functions xSicKxBot 0 6 Yesterday, 08:02 AM
Last Post: xSicKxBot
  [Tut] Show Don’t Tell: Commenting Python Best Practices xSicKxBot 0 4 11-27-2020, 03:41 AM
Last Post: xSicKxBot
  [Tut] How to Catch and Print Exception Messages in Python xSicKxBot 0 7 11-23-2020, 11:45 PM
Last Post: xSicKxBot
  [Tut] Python next() xSicKxBot 0 8 11-22-2020, 08:17 AM
Last Post: xSicKxBot
  [Tut] Python Math Domain Error (How to Fix This Stupid Bug) xSicKxBot 0 11 11-21-2020, 08:01 AM
Last Post: xSicKxBot
  [Tut] Python Int to String with Leading Zeros xSicKxBot 0 18 11-17-2020, 03:22 AM
Last Post: xSicKxBot
  [Tut] Python Function Call Inside List Comprehension xSicKxBot 0 25 11-16-2020, 02:42 AM
Last Post: xSicKxBot
  [Tut] Python TypeError: Object is Not Subscriptable (How to Fix This Stupid Bug) xSicKxBot 0 24 11-13-2020, 12:04 AM
Last Post: xSicKxBot
  [Tut] How to Generate Text Automatically With Python? A Guide to the DeepAI API xSicKxBot 0 34 11-10-2020, 06:23 AM
Last Post: xSicKxBot

Forum Jump:

[-]
Active Threads
(Indie Deal) FREE Brigade E5, Big Bandai...
Last Post: xSicKxBot
Today 07:40 PM
» Replies: 0
» Views: 0
News - Fall Guys developer Mediatonic op...
Last Post: xSicKxBot
Today 05:36 PM
» Replies: 0
» Views: 2
News - Tinybuild sinks $3 million into S...
Last Post: xSicKxBot
Today 05:36 PM
» Replies: 0
» Views: 2
News - Nintendo Shares Tips On How To Ke...
Last Post: xSicKxBot
Today 02:43 PM
» Replies: 0
» Views: 1
News - Random: Amateur Dev Releases Reim...
Last Post: xSicKxBot
Today 02:43 PM
» Replies: 0
» Views: 1
[Tut] Python abs() Function
Last Post: xSicKxBot
Today 09:45 AM
» Replies: 0
» Views: 5
News - It Looks Like Kadabra Can Finally...
Last Post: xSicKxBot
Today 07:46 AM
» Replies: 0
» Views: 7
News - Bungie Cancels Destiny 2's Trials...
Last Post: xSicKxBot
Today 07:46 AM
» Replies: 0
» Views: 5
(Indie Deal) ?U+Me Black Friday Adult Bu...
Last Post: xSicKxBot
Today 06:42 AM
» Replies: 0
» Views: 4
[1.06] BO4 Zombie Trainer (Call of duty ...
Last Post: theolikeappels
Today 01:00 AM
» Replies: 31
» Views: 1189

[-]
Twitter

[-]
Sponsored
Get the Deal of the Week at RefurBees.com

Copyright © SickGaming.net 2012-2020