How to Use Classes and Objects in Python
In Python, classes are used to build objects that will run on methods inside the same class. Classes and objects are a fundamental part of building structured, object-orientated programs to avoid code repetition in Python and other popular programming languages.
In this tutorial, we will learn how to use classes and objects in Python by making a program that will display information about people.
What the Program Will Do
We are going to make a basic program that will display a list of information about different people. These attributes will include their name, age, hair color and eye color.
Each person will become an object and each of their attributes will be a property of that object. We will make the object inside of a class and call a function inside the same class to display information about each person.
How to Create a Class
The first step is to create a class. To create a class type class
followed by the name of the class with the first letter capitalised and a :
(colon) at the end. For this program, we will call the class Person
.
class Person:
Now let's define a function inside the Person
class that will print the details of a person when called. We will also pass in the argument self
as the first parameter of the function.
class Person:
def display_info(self):
print('Name: ' + self.name)
print('Age: ', self.age)
print('Hair: ' + self.hairColor)
print('Eye: ' + self.eyeColor)
Functions in classes are used as methods of an object. The reason why we have to pass self
as an argument of the function is so data from the rest of the object will be available to use inside it.
Create a New Object from the Class
Now the class has been defined we can make a new object using it. This is done by using the name of the class followed by ()
(parenthesis) and storing the new object in a variable.
p1 = Person()
Now we can start adding data to the object variable using a .
(dot) followed by the name of the property and finally its value.
p1.name = 'john'
The code so far should look like this:
class Person:
def display_info(self):
print('Name: ' + self.name)
print('Age: ', self.age)
print('Hair: ' + self.hairColor)
print('Eye: ' + self.eyeColor)
p1 = Person()
p1.name = 'john'
Call a Function in a Class
We can call the display_info
function VIA the p1
object just like other properties except we will include ()
(parenthesis) at the end.
p1.display_info()
Let's run the whole program and check the output:
class Person:
def display_info(self):
print('Name: ' + self.name)
print('Age: ', self.age)
print('Hair: ' + self.hairColor)
print('Eye: ' + self.eyeColor)
p1 = Person()
p1.name = 'john'
p1.age = 28
p1.hairColor = 'brown'
p1.eyeColor = 'blue'
p1.display_info()
Name: john
Age: 28
Hair: brown
Eye: blue
How to Create a Constructor
In the example above we were creating the object properties manually outside of the class. This might be OK if we were only ever going to create one object, but otherwise, the code will get bloated and there will be room for human error.
To solve that problem we can build objects inside the Person
class using a custom constructor function. To create a constructor function we will create a function inside the class called __init__
and pass in arguments for the person data. Inside __init__
we will then set the object properties on self
.
class Person:
def __init__(self, name, age, hairColor, eyeColor):
self.name = name
self.age = age
self.hairColor = hairColor
self.eyeColor = eyeColor
Now we can create a new object by passing the data in as arguments like this:
p1 = Person('John', '28', 'brown', 'blue')
The whole refactored program now looks like this:
class Person:
def __init__(self, name, age, hairColor, eyeColor):
self.name = name
self.age = age
self.hairColor = hairColor
self.eyeColor = eyeColor
def display_info(self):
print('Name: ' + self.name)
print('Age: ', self.age)
print('Hair: ' + self.hairColor)
print('Eye: ' + self.eyeColor)
p1 = Person('John', 28, 'brown', 'blue')
p2 = Person('Emma', 25, 'blonde', 'blue')
p1.display_info()
p2.display_info()
Name: John
Age: 28
Hair: brown
Eye: blue
Name: Emma
Age: 25
Hair: blonde
Eye: blue
Conclusion
You now have an understanding of how classes and objects work together in Python. Classes and objects are a focal point of programming in Python and mastering them is a core discipline of the language.