Saral Code logo
Saral Code
    HomeBlogTutorialsMCQs

No Related Topics Available


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.

Classes

  • A class is a blueprint or a template for creating objects.
  • It defines the properties and methods of an object.
  • Classes are the building blocks of object-oriented programming in Python.

Properties

  • Properties are the characteristics of an object.
  • They can be either instance variables or class variables.

Methods

  • Methods are functions that are associated with an object.
  • They are used to perform actions on the object.

Syntax

The Syntax for Defining a Class is

Python
class ClassName:
    # class body
    # ...

Objects

  • Objects are instances of classes.
  • To create an object, we use the class name followed by parentheses.

Syntax

The Syntax for Creating an Object

Python
object_name = ClassName()

The 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:

Python
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.

Example 1

Python
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()

Example 2

Python
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.