Introduction
Classes are the foundation of object orient programming (OOP). Class is a blueprint for creating objects. It defines a set of attributes (data) and methods (functions) that objects of that class will have. They allow you to model real world entities, encapsulate data & define behavior. Python offers different ways to define classes to suit various scenarios. This blog will explore the different types of classes you can create in Python.
Standard Classes: It is the most common type of class, defined using the class keyword. It is used to model objects with data and functions.
class ProgrammingClass:
# Constructor to initialize attributes
def __init__(self, code_name):
self.code_name = code_name
# Method to display name
def display(self):
print(f"Welcome to the {self.code_name} class.")
# Create a instance of class
p = ProgrammingClass("Python")
p.display()
Abstract Classes: Abstract classes are used to define a blueprint for other classes. They cannot be instantiated directly and typically contain one or more abstract methods.
from abc import ABC, abstractmethod
# abstract class
class Shape(ABC):
# abstract method
@abstractmethod
def area(self):
pass
@abstractmethod
def perimeter(self):
pass
class Rectange(Shape):
def __init__(self,width,height):
self.width = width
self.height = height
def area(self):
return self.width * self.height
def perimeter(self):
return 2*(self.width + self.height)
r = Rectange(4,5)
print(f"area is {r.area()}")
print(f"perimeter is {r.perimeter()}")
Static Classes: Static functions are defined using the @staticmethod decorator and do not operate on an instance or class directly.
class MathClass:
@staticmethod
def add_num(a,b):
return a+b
@staticmethod
def mul_num(a,b):
return a*b
# call static methods directly on class
print(f"Addition of 2 number is {MathClass.add_num(4,3)}")
print(f"Multiplication of 2 number is {MathClass.mul_num(4,3)}")
Classes with class functions: Class functions are defined using the @classmethod decorator and operate on the class itself rather than an instance.
class Employee:
company_name = "HCSTech"
def __init__(self, name, section):
self.name = name
self.section = section
@classmethod
def set_company_name(cls,name):
cls.company_name = name
Employee.set_company_name("InnovateTech")
print(Employee.company_name)
Nested Classes: A class can be defined inside another class known as a nested class.
class Outer:
class Inner:
def display(self):
print("This is a nested class.")
# create an instance of the method class
nested = Outer.Inner()
nested.display()
Data Classes: Simplify the creation of classes that primarily store data. Use the @dataclass decorator from the dataclasses module to generate boilerplate code like __init__, __repr__, __eq__.
from dataclasses import dataclass
@dataclass
class Product:
name: str
price: float
p = Product("Laptop", 1200.91)
print(p.name)
Enum Classes: Enum classes are used to define a set of named constant values. Use the enum module to create them.
from enum import Enum
class Color(Enum):
RED = 1
GREEN = 2
BLUE = 3
print(Color.RED, Color.RED.name, Color.RED.value)
