top of page

Python is also an object-oriented programming language. So like other object-oriented programming languages classes are used to group related code together and execute using objects.


In python classes are declared with 'class' keyword followed by the class name and ':'. In Python, most of the working of the class is similar to a class in C++. In other words, the python class takes the functionality of the c++ class with the addition of a few more features.

Methods: Functions defined inside a class is known as methods.


Class objects


Class objects are variables used to access the class members and methods.

Class instantiation uses function notation. Just pretend that the class object is a parameterless function that returns a new instance of the class.

The object creates a new instance of the class and assigns this object to the local variable.



The class is always initialized with an __init__() function which acts as a constructor for the class with a 'self' parameter. The __init__() function is used to set up some default values for the methods in the class.

What is the 'self.name' doing here in the method and self in the method parameter?


The self parameter is a prerequisite for any method in the class. Due to the self parameter whenever a new object is created and a new instance of the variable is created. That means the previous operation due to a previous object does not affect the variable value of the next object.

You'll need to pass "self" to any class functions as the first argument if you want them to behave as non-static methods. "self" are instance variables for your class.

It is not necessary the first parameter of any method should be self it could be named anything and can be different for each method in the class.

But all the programmers 'self' because it is easy to understand by another programmer working on the same code.

Methods may call other methods by using method attributes of the self argument (you will see all of this in the below examples)

Below is a code example where the code converts the time of epoch into seconds:


Python time method time.time() returns the time as a floating-point number expressed in seconds since the epoch, in UTC.

Inheritance

Inheritance gives the properties to the inherited class from which it is inherited from.

Now to create a child class or a derived class you have to send the parent class or derived class name as the parameter while defining the class.

Adding the __init__() function for the derived class to give initializations for its member methods.

The addition of the __init__() function will override the inheritance of __init__() function of the parent class.

Now if you want to keep the inheritance of the parent class or the derived class. Call the parents function __init__() function from the derived class __init__() function.

Super() Function

To inherit all the methods and properties from the parent class the super() function is used.


12 views1 comment

Recent Posts

See All

1 Comment


Adithya Shetty
Adithya Shetty
Dec 28, 2019

Ask anything regarding the post. Or head to the forum

Like

Post Title

Thanks for submitting!

Rate UsDon’t love itNot greatGoodGreatLove itRate Us
Python Classes

Thursday, 26 December, 2019

Python Functions

Friday, 29 November, 2019

Now You Can Blog from Everywhere!

Saturday, 28 September, 2019

Design a Stunning Blog

Saturday, 28 September, 2019

Grow Your Blog Community

Saturday, 28 September, 2019

Recent Posts

bottom of page