Extra 5% OFF Use Code: OL05
Free Shipping over ₹999

Classes/Objects

Class

A class is a blueprint for creating objects. It defines attributes (variables) and methods (functions) that describe the behavior of the object.

Create a Class

class ClassName:
    # Constructor to initialize attributes
    def __init__(self):
        self.attribute = "Value"

    # Method (Function inside class)
    def method_name(self):
        print("This is a method inside the class.")

Class Properties:

  • Encapsulation – Groups data (attributes) and behavior (methods) together.
  • Abstraction – Hides implementation details from the user.
  • Inheritance – Allows a class to inherit properties from another class.
  • Polymorphism – Allows different classes to use the same method in different ways.

Object

An object is an instance of a class. It represents a real-world entity with specific values assigned to its attributes.

Create a Object

object_name = ClassName()

Object Properties:

  • Identity – A unique memory location.
  • State – Defined by attributes (variables).
  • Behavior – Defined by methods (functions).

Example of Class and Object

# Defining a class
class Car:
    def __init__(self, brand, model, year):
        self.brand = brand  # Attribute
        self.model = model  # Attribute
        self.year = year    # Attribute

    def display(self):  # Method
        print(f"Car: {self.brand} {self.model}, Year: {self.year}")

# Creating objects
car1 = Car("Toyota", "Corolla", 2022)
car2 = Car("Honda", "Civic", 2023)

# Accessing properties
car1.display()
car2.display()

Output:

Car: Toyota Corolla, Year: 2022
Car: Honda Civic, Year: 2023

Code Explanation:

  1. Defining a Class
class Car:
  • The class keyword is used to define a class named Car.
  • A class is a blueprint for creating objects.

2. Using the __init__ Constructor

def __init__(self, brand, model, year):
    self.brand = brand  # Attribute
    self.model = model  # Attribute
    self.year = year    # Attribute
  • __init__ is a special method (constructor) that initializes the object’s attributes when it is created.

Attributes:

  • self.brand stores the car brand.
  • self.model stores the car model.
  • self.year stores the manufacturing year.

3. Defining a Method (display)

def display(self):  
    print(f"Car: {self.brand} {self.model}, Year: {self.year}")
  • A method (display) is created to print the details of the car.
  • It uses an f-string (f"...") to format and print car attributes.

4. Creating Objects

car1 = Car("Toyota", "Corolla", 2022)
car2 = Car("Honda", "Civic", 2023)

car1 is an object of Car with:

  • brand = "Toyota"
  • model = "Corolla"
  • year = 2022

car2 is an object of Car with:

  • brand = "Honda"
  • model = "Civic"
  • year = 2023

5. Calling Methods

car1.display()
car2.display()

Each object’s display() method prints its details.

Object Method

  1. Creating Multiple Objects

You can create multiple objects from the same class.

car2 = Car("Honda", "Civic")
car3 = Car("Ford", "Mustang")

car2.display()
car3.display()

Output:

Car: Honda Civic
Car: Ford Mustang

2. Accessing Object Properties

You can access attributes using the dot (.) operator.

print(car1.brand)  # Output: Toyota
print(car1.model)  # Output: Corolla

3. Modifying Object Properties

car1.brand = "BMW"
print(car1.brand)  # Output: BMW

4. Deleting an Object

You can delete an object using del.

del car1

    Leave a Reply

    Your email address will not be published.

    Need Help?