Python Class and object

Python supports object-oriented programming (OOP), where classes and objects are foundational concepts. Here’s a comprehensive overview of how classes and objects work in Python.

Classes in Python

A class is a blueprint for creating objects. It encapsulates data (attributes) and methods (functions) that operate on the data. Essentially, a class defines a new data type with its associated properties and behaviors.

Syntax:

class ClassName:
    # Statement suite

ClassName: The name of the class, which should follow the standard naming conventions.

Statement suite: Contains the attributes, methods, and constructor.

Example:

class Building:
    """A class representing a building."""

    def __init__(self, floors, rooms):
        self.floors = floors
        self.rooms = rooms

    def display_info(self):
        print(f"Floors: {self.floors}, Rooms: {self.rooms}")

In this example, Building is a class with an __init__ constructor to initialize attributes and a method display_info to print the attributes.

Creating Objects

An object is an instance of a class. Once a class is defined, you can create multiple objects (instances) of that class, each with its unique attributes.

Syntax:

object_name = ClassName(arguments)

Example:

# Create an instance of the Building class
my_building = Building(floors=3, rooms=10)
my_building.display_info() # Output: Floors: 3, Rooms: 10

The self Parameter

The self parameter in class methods refers to the instance of the class. It is used to access variables and methods associated with the class instance.

Example:

class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def greet(self):
        print(f"Hello, my name is {self.name} and I am {self.age} years old.")

person = Person("Alice", 30)
person.greet()     # Output: Hello, my name is Alice and I am 30 years old.

The __init__ Method

The __init__ method is a special method called a constructor. It initializes the instance of the class. This method is automatically called when a new instance is created.

class Car:
    def __init__(self, make, model):
        self.make = make
        self.model = model

    def describe(self):
        print(f"This car is a {self.make} {self.model}.")

my_car = Car("Toyota", "Corolla")
my_car.describe()     # Output: This car is a Toyota Corolla.

Class and Instance Variables

Class Variables: Shared by all instances of the class. They are defined within the class but outside any methods.

Instance Variables: Unique to each instance. They are defined inside methods, typically in the __init__ method.

Example:

class School:
    school_name = "Greenwood High" # Class variable

    def __init__(self, student_name):
        self.student_name = student_name # Instance variable

    def show_details(self):
        print(f"School: {School.school_name}, Student: {self.student_name}")

student1 = School("John")
student2 = School("Emily")

student1.show_details()     # Output: School: Greenwood High, Student: John
student2.show_details()     # Output: School: Greenwood High, Student: Emily

In this example:

school_name is a class variable shared by all instances.

student_name is an instance variable unique to each instance.

Summary

Classes are blueprints for creating objects. They encapsulate data and methods that operate on the data.

Objects are instances of classes, created with the class constructor (__init__).

self refers to the instance of the class and is used to access its attributes and methods.

__init__ is the constructor method that initializes the instance.

Class Variables are shared among all instances, while Instance Variables are unique to each instance.