Classes and Objects in Python
Object-Oriented Programming (OOP) • What is OOP? • Programming paradigm focusing on objects rather than just functions + data • Objects encapsulate data (attributes) and functions (methods) • Benefits of OOP • Modularity, reusability, maintainability • Abstraction, encapsulation, inheritance, polymorphism
Classes and Objects • A class is a new data type • Objects are instances of a class class Shape: def __init__(self, name, num_sides): self.name = name self.num_sides = num_sides def describe(self): print(f"I am a {self.num_sides}-sided {self.name}.") triangle = Shape("Triangle", 3) square = Shape("Square", 4) triangle.describe() # Output: I am a 3-sided Triangle. square.describe() # Output: I am a 4-sided Square.
Attributes • Instance attributes • Defined in the __init__ method • Accessible through the self keyword • Class attributes • Defined outside of methods, shared among all instances
Methods • Instance methods • Defined with self parameter • Access and modify instance attributes • Class methods • Defined with cls parameter • Access and modify class attributes • Static methods • No self or cls parameter • Standalone functions within the class class Shape: name = "Shape" def __init__(self, n): self.side = n @classmethod def get_class_name(cls): return cls.name @staticmethod def add(x, y): return x + y @staticmethod def multiply(x, y): return x * y # Usage print(Shape.get_class_name()) # Output: Shape
Inheritance • A class (child class, subclass) can inherit all methods and attributes of another class (parent class, super class) class Polygon(Shape): def __init__(self, name, n, a): super().__init__(name, n) self.angle = a def interior_angle(self): print(f"The interior angle of a {self.name} is {self.angle} degrees.") pol = Polygon("Hexagon", 6, 60) pol.describe()
Polymorphism • Objects of different classes can be treated as the same type • Example: describe() method for different shapes class Circle(Shape): def __init__(self, radius): super().__init__("Circle", 0) self.radius = radius def describe(self): print(f"I am a circle with a radius of {self.radius} units.") shapes = [Shape("Square", 4), Polygon("Pentagon", 5, 108), Circle(7)] for shape in shapes: shape.describe()
Encapsulation • Hiding implementation details from outside users • Private attributes and methods Prefixed with __ (double underscore) • Non accessible from outside the class • Protected attributes and methods Prefixed with _ (single underscore) • Accessible outside the class but meant for internal use only class Rectangle(Shape): def __init__(self, length, width): super().__init__("Rectangle", 4) self.__length = length self.__width = width def get_area(self): return self.__length * self.__width def set_length(self, new_length): self.__length = new_length def set_width(self, new_width): self.__width = new_width
Conclusion • OOP is a powerful programming paradigm in Python • Classes, objects, inheritance, polymorphism, and encapsulation are key concepts • OOP promotes modularity, reusability, and maintainability in code

OOP in Python Programming: Classes and Objects

  • 1.
  • 2.
    Object-Oriented Programming (OOP) •What is OOP? • Programming paradigm focusing on objects rather than just functions + data • Objects encapsulate data (attributes) and functions (methods) • Benefits of OOP • Modularity, reusability, maintainability • Abstraction, encapsulation, inheritance, polymorphism
  • 3.
    Classes and Objects •A class is a new data type • Objects are instances of a class class Shape: def __init__(self, name, num_sides): self.name = name self.num_sides = num_sides def describe(self): print(f"I am a {self.num_sides}-sided {self.name}.") triangle = Shape("Triangle", 3) square = Shape("Square", 4) triangle.describe() # Output: I am a 3-sided Triangle. square.describe() # Output: I am a 4-sided Square.
  • 4.
    Attributes • Instance attributes •Defined in the __init__ method • Accessible through the self keyword • Class attributes • Defined outside of methods, shared among all instances
  • 5.
    Methods • Instance methods •Defined with self parameter • Access and modify instance attributes • Class methods • Defined with cls parameter • Access and modify class attributes • Static methods • No self or cls parameter • Standalone functions within the class class Shape: name = "Shape" def __init__(self, n): self.side = n @classmethod def get_class_name(cls): return cls.name @staticmethod def add(x, y): return x + y @staticmethod def multiply(x, y): return x * y # Usage print(Shape.get_class_name()) # Output: Shape
  • 6.
    Inheritance • A class(child class, subclass) can inherit all methods and attributes of another class (parent class, super class) class Polygon(Shape): def __init__(self, name, n, a): super().__init__(name, n) self.angle = a def interior_angle(self): print(f"The interior angle of a {self.name} is {self.angle} degrees.") pol = Polygon("Hexagon", 6, 60) pol.describe()
  • 7.
    Polymorphism • Objects ofdifferent classes can be treated as the same type • Example: describe() method for different shapes class Circle(Shape): def __init__(self, radius): super().__init__("Circle", 0) self.radius = radius def describe(self): print(f"I am a circle with a radius of {self.radius} units.") shapes = [Shape("Square", 4), Polygon("Pentagon", 5, 108), Circle(7)] for shape in shapes: shape.describe()
  • 8.
    Encapsulation • Hiding implementationdetails from outside users • Private attributes and methods Prefixed with __ (double underscore) • Non accessible from outside the class • Protected attributes and methods Prefixed with _ (single underscore) • Accessible outside the class but meant for internal use only class Rectangle(Shape): def __init__(self, length, width): super().__init__("Rectangle", 4) self.__length = length self.__width = width def get_area(self): return self.__length * self.__width def set_length(self, new_length): self.__length = new_length def set_width(self, new_width): self.__width = new_width
  • 9.
    Conclusion • OOP isa powerful programming paradigm in Python • Classes, objects, inheritance, polymorphism, and encapsulation are key concepts • OOP promotes modularity, reusability, and maintainability in code