Python – Method Overriding

Click the link: “Python – Method Overriding

In this tutorial we will learn about method overriding in Python.

What is method overriding?

When a method in a child class has the same name and type signature as a method in the parent class then the child class method is said to override the parent class method and this is method overriding.

Example of method overriding

In the following Python program we are overriding theĀ greetingsĀ method of the parent classĀ AwesomeĀ in the child classĀ SuperAwesome.

# parent class
class Awesome:

    def greetings(self, message = "Hello World"):
        print("Greetings from Awesome:", message)

# child class
class SuperAwesome(Awesome):

    # overriding the method of the parent class
    def greetings(self, message = None):
        if message != None:
            print("Greetings from SuperAwesome:", message)
        else:
            print("Greetings from SuperAwesome!")

# objects
pObj = Awesome()        # parent class object
cObj = SuperAwesome()   # child class object

# method call
pObj.greetings()
pObj.greetings('Hello!!!')

cObj.greetings()
cObj.greetings('Hello!!!')

Read more…

Python – Class Destructor __del__ method

Click the link: “Python – Class Destructor __del__ method

In this tutorial we will learn about the classĀ __del__Ā method in Python.

We learned about classes and objects in theĀ Python – Classes and ObjectsĀ tutorial. Feel free to check that out.

TheĀ __del__Ā method

TheĀ __del__Ā method is a special method of a class.

It is also called the destructor method and it is called (invoked) when the instance (object) of the class is about to get destroyed.

We use theĀ __del__Ā method to clean up resources like closing a file.

In the following Python program we are creating theĀ __del__Ā method inside theĀ Awesomeclass.

# class
class Awesome:

    # some method
    def greetings(self):
        print("Hello World!")

    # the del method
    def __del__(self):
        print("Hello from the __del__ method.")

# object of the class
obj = Awesome()

# calling class method
obj.greetings()

The above code will print the following output.

Hello World!
Hello from the __del__ method.

Read more…

Python – Class Constructor __init__ method

Click the link: “Python – Class Constructor __init__ method

In this tutorial we will learn about the classĀ __init__Ā method in Python.

We learned about classes and how to create objects using classes in theĀ Python – Classes and ObjectsĀ tutorial. Feel free to check that out.

TheĀ __init__Ā method

TheĀ __init__Ā method is a special method of a class.

It is also called the constructor method and it is called when we create (instantiate) an object of the class.

We use theĀ __init__Ā method to initialise class attributes or call class methods.

In the following Python program we are creating theĀ __init__Ā method inside theĀ Awesomeclass.

# class
class Awesome:

    # the init method
    def __init__(self):
        print("Hello from the __init__ method.")

# object of the class
obj = Awesome()

Read more…