Classes and objects are two fundamental concepts of object-oriented programming (OOP) in Python. In this blog, we will learn about these concepts in detail with examples and understand how they work in Python.
Syntax
The Syntax for Defining a Class is
class ClassName:
# class body
# ...
Syntax
The Syntax for Creating an Object
object_name = ClassName()
self
keyword🤔The self
keyword in Python refers to the instance of a class. In other words, it is a reference to the object that is created from a class.
When a method is called on an object, the instance of the object is automatically passed as the first argument to the method, and this argument is stored in the self
variable. By using self
, you can access the attributes and methods of the object within the method.
For example, consider the following code:
class Example:
def display(self, value):
self.value = value
print(self.value)
obj = Example()
obj.display(10) # 10
print(obj.value) # 10
Here, the display
method takes two arguments: self
and value
. When the method is called on the object obj
, the instance of the object obj
is passed as the first argument, which is stored in self
, and the value
argument is set to 10
. The self.value
attribute is then set to 10
and printed.
In short, the self
keyword is used to access the instance of an object within a method, allowing you to access its attributes and methods.
class Vehicle:
# class properties
vtype = ""
make = ""
model = ""
# method to set type
def set_type(self, vtype):
self.vtype = vtype
# method to set make
def set_make(self, make):
self.make = make
# method to set model
def set_model(self, model):
self.model = model
# method to display vehicle properties
def display(self):
print("Type:", self.vtype)
print("Make:", self.make)
print("Model:", self.model)
# create first vehicle object
vehicle_1 = Vehicle()
vehicle_1.set_type("Car")
vehicle_1.set_make("Toyota")
vehicle_1.set_model("Camry")
vehicle_1.display()
# create second vehicle object
vehicle_2 = Vehicle()
vehicle_2.set_type("Bike")
vehicle_2.set_make("Harley Davidson")
vehicle_2.set_model("Road King")
vehicle_2.display()
class Book:
# class properties
title = ""
author = ""
ISBN = ""
# method to set title
def set_title(self, title):
self.title = title
# method to set author
def set_author(self, author):
self.author = author
# method to set ISBN
def set_ISBN(self, ISBN):
self.ISBN = ISBN
# method to display book properties
def display(self):
print("Title:", self.title)
print("Author:", self.author)
print("ISBN:", self.ISBN)
# create first book object
book_1 = Book()
book_1.set_title("The Great Gatsby")
book_1.set_author("F. Scott Fitzgerald")
book_1.set_ISBN("123456789")
book_1.display()
# create second book object
book_2 = Book()
book_2.set_title("To Kill a Mockingbird")
book_2.set_author("Harper Lee")
book_2.set_ISBN("987654321")
book_2.display()
In conclusion, classes and objects are essential components of object-oriented programming in Python. Classes provide a blueprint for creating objects and objects are instances of classes. Understanding classes and objects will help us to develop object-oriented applications in Python.