Python Classes and Objects With Examples

Python is an object-oriented programming language. Python Classes and Objects are the core building blocks of Python programming language.

Python Class

By this time, all of you should have already learned about Python Data Types. If you remember, basic data types in python refer to only one kind of data at a time.

How would it be if you could declare a data type which itself contains more than one data types and can work with them with the help of any function? Python class gives you that opportunity.

Python class is the blueprint on which instances of the class are created.

Simple Python Class Declaration

Here’s the very basic structure of python class definition.

Now, let’s work with real examples.

This example is pretty much self-explanatory. As we know, the lines starting with “#” are python comments. The comments explain the next executable steps. This code produces the following output.

python-class-example

Python Class Definition

This line marks the beginning of class definition for class ‘Person’.

Python Class Variables

‘name’ and ‘age’ are two member variables of the class ‘Person’. Every time we declare an object of this class, it will contain these two variables as its member. This part is optional as they can be initialized by the constructor.

Python Class Constructor

Python class constructor is the first piece of code to be executed when you create a new object of a class.

Primarily, the constructor can be used to put values in the member variables. You may also print messages in the constructor to be confirmed whether the object has been created.

We shall learn a greater role of constructor once we get to know about python inheritance. The constructor method starts with def __init__. Afterward, the first parameter must be ‘self’, as it passes a reference to the instance of the class itself. You can also add additional parameters like the way it is shown in the example. ‘personName’ and ‘personAge’ are two parameters to sent when a new object is to be created.

Python Class Methods

Methods are declared in the following way:

In the pre-stated example, we’ve seen that the method showName() prints value of ‘name’ of that object. We shall discuss a lot more about python methods some other day.

Python Class Object

The way objects are created in python is quite simple. At first, you put the name of the new object which is followed by the assignment operator and the name of the class with parameters (as defined in the constructor).

Remember, the number and type of parameters should be compatible with the parameters received in the constructor function.

When the object has been created, member methods can be called and member attributes can be accessed (provided they are accessible).

That’s all for the basics of python class. As we are going to learn about python object-oriented features like inheritance, polymorphism in the subsequent tutorials, we shall learn more about python class and its features. Till then, happy coding and good bye! Feel free to comment if you have any query.

Reference: Python.org Documentation

By admin

Leave a Reply