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…

Advertisements

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…

Python – time Module

Click the link: “Python – time Module

In this tutorial we will learn about time module in Python.

We learned about modules in the Python – Modules tutorial. Feel free to check that out.

In one of the previous tutorial Python – datetime Module we learned how to work with date and time using the datetime module.

Importing the time module

To work with time we have to first import the time module by writing the following line.

import time

Read more…