In chapter 13 of Python Tutorial, we will learn about Classes and Objects
If you are reading this tutorial on Mobile, please rotate your mobile horizontally for best reading experience. You can enable auto-rotate in your phone settings.
In previous tutorials, you learnt about built-in data types like dict
and list
. Do you know that we can define our own data types in Python? These data types are called classes and when an instance of a class is created, it is called an object.
While creating classes, we can bundle variables and functions together which will make it easy for us to use, reuse and further manipulate the classes according to our requirement.
A class is created by using the keyword class, followed by the class name. The basic syntax is as below:
class ClassName: <statement 1> . . . <statement n>
Let’s look at an basic example of a class:
#defining a class class Employee: name = "" age = 0
In the above example, we have created a class with the name Employee. “name” and “age” are the variables inside the class. Variables, which are defined inside the class, are called attributes.
Python Object:
As mentioned earlier, objects are instances of a class. Let’s look at an example:
#defining a class class Employee: name = "" age = 0 #creating an object of the class emp1 = Employee() #We can create multiple objects from a single class. emp2 = Employee() emp3 = Employee() . . . empn = Employee()
In the above example, emp1, emp2, emp3 and so on… are the objects of the class. Using these objects, we can access class attributes, “name” and “age”
Check the below example on how to access class attributes using object:
# accessing and modifying name attribute emp1.name = "Alex" # accessing the age attribute emp1.age = 28
Let’s bring all the above examples together to create a class and object, and then print an output. Check below:
class Employee:
name = ""
age = 0
#creating object
emp1 = Employee()
#accessing class attributes and modifying the values as per our requirements using the object
emp1.name = "Alex"
emp1.age=28
#let's print the output
print("Employee %s is %d years old" %(emp1.name,emp1.age))
As you can see above, we have defined the class named Employee
with two class attributes: name and age.
We have created an object emp1 of the class Employee
.
Finally, we have accessed and modified the values of the object using the .
notation.
Python Method:
As mentioned earlier, we can bundle variables and functions together while creating a class. We have already seen above examples for variables. Now, let’s define function inside a class.
A function defined inside a class is called a method. Let’s look at the below example:
class Employee:
name = ""
age = 0
salary = 0.0
location = ""
#defining function
def data(self):
print("Employee %s is %d years old, drawing a salary of $%.2f and current location is %s" %(self.name,self.age,self.salary,self.location))
#creating object
emp1 = Employee()
#accessing class attributes and modifying the values as per our requirements using the object
emp1.name = "Alex"
emp1.age = 28
emp1.salary = 6500.50
emp1.location = "Texas"
emp1.data()
In the above example. “Self” parameter refers to the instance of the class that is currently being used. By using the “Self” parameter, we can access the attributes and methods of the class.
init()
The __init__() is a special function, often referred to as constructor. This special function is called whenever the class is initiated (i.e.., creating an object). For example:
class Employee: def __init__(self, name, age): # This is the constructor that is called when creating a new object(emp1) # It takes two parameters, name and age, and initializes them as attributes of the object self.name = name self.age = age emp1 = Employee()
Confused? Ok, Let’s look at a complete program which has the constructor, object and method to provide better clarity:
class Employee:
def __init__(self,name,age):
self.name = name
self.age = age
def data(self):
print("Employee %s is %d years old" %(self.name,self.age))
emp1 = Employee("Alex", 28)
emp1.data()
In the above example, we used __init__() constructor which takes two parameters name and age, and initializes them as attributes of the object.
When we are using a constructor, we need to pass the corresponding values during the object creation of the class and this is what we have done in the above example. Passing the values “Alex” and “28” when creating the object emp1.
In simple terms, __init__() is used for assigning values in a class.
Assignment:
Create two objects emp1 and emp2 from class Employee and assign the below values:
“Alex”, 28, 3500.00, “Texas”
“Smith”, 35, 5000.00, “Florida”
After object creation, call the method data from class Employee for emp1 and emp2.
class Employee:
def __init__(self,name,age,salary,location):
self.name = name
self.age = age
self.salary = salary
self.location = location
def data(self):
print("Employee %s is %d years old, drawing a salary of $%.2f and current location is %s" %(self.name, self.age, self.salary, self.location))
#write your code here
class Employee:
def __init__(self,name,age,salary,location):
self.name = name
self.age = age
self.salary = salary
self.location = location
def data(self):
print("Employee %s is %d years old, drawing a salary of $%.2f and current location is %s" %(self.name, self.age, self.salary, self.location))
#write your code here
emp1 = Employee("Alex", 28, 3500.00, "Texas")
emp2 = Employee("Smith", 35, 5000.00, "Florida")
emp1.data()
emp2.data()
That brings to the end of chapter 13 of Python tutorial, see you in the next tutorial!!!
By the way, if you haven’t installed Python and Pycharm IDE on your Windows laptop, you can visit the below links to learn on how to install and configure/install Python and Pycharm IDE on Windows OS. This might be helpful in understanding the Python language easier.
How to Install Python 3 and Pycharm IDE on Windows
How to configure Pycharm IDE and “Run” your first Python 3 program
For your reference, to download Pycharm IDE, visit below link:
https://www.jetbrains.com/pycharm/download/?section=windows
However, if you don’t want to go through installation process, you can practice your learnings on this page itself by using integrated Python IDE above.
If you have any questions or comments about chapter 13 of Python tutorial, please let us know in the comments section below.