Hello everyone, hope you are doing fine. Today we are going to learn about the main backbone behind everything that is beautiful in Python, that is python class system. Object oriented programming features are amazing in python, which of course, we will learn in subsequent lessons.
Python Class is at the centre of the whole object oriented concept. In fact, object itself is derived from class. Before we start, you may have a look at our previous write-ups on Python List Comprehension and Python Sort List in case you missed those. Now, grab a coffee and let’s ride through Python 3 class system.
Table of Contents
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. Let’s see it for ourselves.
Simple Python Class Declaration
Here’s the very basic structure of python class definition.
Copyclass ClassName: # list of python class variables # python class constructor # python class method definitions
Now, let’s work with real examples.
Copy#definition of the class starts here class Person: #initializing the variables name = "" age = 0 #defining constructor def __init__(self, personName, personAge): self.name = personName self.age = personAge #defining class methods def showName(self): print(self.name) def showAge(self): print(self.age) #end of class definition # Create an object of the class person1 = Person("Richard", 23) #Create another object of the same class person2 = Person("Anne", 30) #call member methods of the objects person1.showAge() person2.showName()
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 Definition
This line marks the beginning of class definition for class ‘Person’.
Python Class Variables
Copy#initializing the variables name = "" age = 0
‘name’ and ‘age’ are two member variables of the class ‘Person’. Everytime we declare an object of this class, it will contain this two variables as its member. This part is optional as they can be initialized by the constructor.
Python Class Constructor
Copy#defining constructor def __init__(self, personName, personAge): self.name = personName self.age = personAge
Python class constructor is the first piece of code to be executed when you create a new object of a class.
Primarily, 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__. Afterwards 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
Copy#defining python class methods def showName(self): print(self.name)
Methods are declared in the following way:
Copydef method_name(self, parameter 1, parameter 2, …….) statements…….. return value (if required)
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
Copy# Create an object of the class person1 = Person("Richard", 23) #Create another object of the same class person2 = Person("Anne", 30) #call member methods of the objects person1.showAge() person2.showName()
The way objects are created in python is quite simple. At first, you put the name of the new object which is followed by 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).
Copy#print the name of person1 by directly accessing the ‘name’ attribute print(person1.name)
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