Python – Method Overloading

Click the link: “Python – Method Overloading

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

We learned about method overriding in the Python – Method Overriding tutorial. Feel free to check that out.

What is method overloading?

In Python we can create a method that can be called in different ways.

So, we can have a method that has zero, one or more number of parameters and depending on the method definition we can call it with zero, one or more arguments.

This is method overloading in Python.

Read more…

Advertisements

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 – Inheritance

Click the link: “Python – Inheritance

In this tutorial we will learn about inheritance in Python.

What is inheritance?

Inheritance is one of the core concepts of OOP – Object Oriented Programming. It helps us to create hierarchy.

Inheritance is a concept were a child class inherits the properties and methods from the parent class.

Super class and Sub class

The class that is used to create other classes is called the parent class or super class.

The class that inherits the attributes and methods of the parent class is called the sub classor child class.

Read more…

Python – Built-in Class Attributes

Click the link: “Python – Built-in Class Attributes

In this tutorial we will learn about built-in class attributes in Python.

Built-in class attributes gives us information about the class.

We can access the built-in class attributes using the . operator.

Following are the built-in class attributes.

Attribute Description
__dict__ This is a dictionary holding the class namespace.
__doc__ This gives us the class documentation if documentation is present. Noneotherwise.
__name__ This gives us the class name.
__module__ This gives us the name of the module in which the class is defined.

In an interactive mode it will give us __main__.

__bases__ A possibly empty tuple containing the base classes in the order of their occurrence.

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…

Python – Classes and Objects

Click the link: “Python – Classes and Objects

In this tutorial we will learn about classes and objects in Python.

Python is an object oriented programming (OOP) language. Everything in Python is an object which consists of some methods and properties (attributes).

What is a class?

A class is a blueprint which is used to create objects. Classes consists of properties (attributes) and methods.

We create classes using the class keyword in Python.

Syntax:

class className:
  #
  # some code goes here...
  #

Where, className is the name of the class.

Read more…