Index
Introduction to Object-Oriented Programming (OOP)
Class and Object
Constructors
Class, Object, and Instance Attributes
Self Parameter
Methods in Object
Static Method
Decorators
1. Introduction to Object-Oriented Programming (OOP)
Definition:
Object-Oriented Programming (OOP) is a programming paradigm that organizes software design around objects rather than functions and logic. Objects can represent real-world entities with attributes (data) and methods (functions) that operate on the data.
Use-Case Example:
OOP is used to model complex systems where different entities (objects) interact. For instance, an e-commerce system with customers, products, and orders can be modeled using objects.
Key Concepts:
Class: A blueprint for creating objects.
Object: An instance of a class.
Encapsulation: Restricting access to the internal state of an object.
Inheritance: Creating new classes from existing ones.
Polymorphism: Using a common interface for different types of objects.
2. Class and Object
Definition:
Class: A blueprint or template for creating objects (instances). It defines the attributes and behaviors of the object.
Object: An instance of a class that holds the actual data and interacts with the class’s methods.
Use-Case Example:
A class Car
defines the blueprint for all cars, while each car object (e.g., my_car
, your_car
) is an instance of the Car
class with specific attributes like color, brand, and model.
Syntax:
class ClassName:
# Class attributes and methods
# Creating an object
obj = ClassName()
Basic Example:
class Car:
def __init__(self, brand, model):
self.brand = brand
self.model = model
# Creating an object of the Car class
my_car = Car("Toyota", "Corolla")
print(my_car.brand, my_car.model) # Output: Toyota Corolla
3. Constructors
Definition:
A constructor is a special method that is automatically called when an object is created. In Python, the constructor is defined by the __init__()
method.
Use-Case Example:
The constructor is used to initialize the attributes of an object when it is created.
Syntax:
class ClassName:
def __init__(self, parameters):
# Initialize attributes
Basic Example:
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
# Creating an object
person = Person("Alice", 30)
print(person.name, person.age) # Output: Alice 30
4. Class, Object, and Instance Attributes
Definition:
Class Attribute: Shared by all instances of the class.
Object (Instance) Attribute: Unique to each object.
Use-Case Example:
Class attributes are used for constants shared by all instances, while instance attributes are specific to individual objects.
Syntax:
class ClassName:
class_attribute = value
def __init__(self, instance_attribute):
self.instance_attribute = instance_attribute
Basic Example:
class Animal:
species = "Mammal" # Class attribute
def __init__(self, name):
self.name = name # Instance attribute
dog = Animal("Dog")
cat = Animal("Cat")
print(dog.species, dog.name) # Output: Mammal Dog
print(cat.species, cat.name) # Output: Mammal Cat
5. Self Parameter
Definition:
The self
parameter is a reference to the current object. It is used to access attributes and methods of the class within its methods.
Use-Case Example:self
allows the methods in a class to refer to the instance calling the method.
Syntax:
class ClassName:
def method(self):
# Access instance variables using self
Basic Example:
class Person:
def __init__(self, name):
self.name = name
def greet(self):
return "Hello, " + self.name
# Creating an object and calling a method
person = Person("Bob")
print(person.greet()) # Output: Hello, Bob
6. Methods in Object
Definition:
A method is a function defined inside a class that operates on the data contained in an object. Methods are used to define the behaviors of an object.
Use-Case Example:
Methods allow objects to perform actions, such as a Car
class having a drive()
method.
Syntax:
class ClassName:
def method_name(self):
# Method body
Basic Example:
class Circle:
def __init__(self, radius):
self.radius = radius
def area(self):
return 3.14 * self.radius ** 2
circle = Circle(5)
print(circle.area()) # Output: 78.5
7. Static Method
Definition:
A static method is a method that does not access or modify the class or instance attributes. It is defined using the @staticmethod
decorator.
Use-Case Example:
Static methods are used when the method logic is independent of class attributes or instance attributes.
Syntax:
class ClassName:
@staticmethod
def static_method():
# Method body
Basic Example:
class Math:
@staticmethod
def add(a, b):
return a + b
print(Math.add(5, 3)) # Output: 8
8. Decorators
Definition:
Decorators are a way to modify or extend the behavior of functions or methods without modifying their code. In Python, decorators are applied using the @decorator_name
syntax.
Use-Case Example:
Decorators are often used to add functionality like logging, access control, or caching to existing functions.
Syntax:
@decorator_name
def function_name():
# Function body
Basic Example:
def decorator(func):
def wrapper():
print("Before function execution")
func()
print("After function execution")
return wrapper
@decorator
def say_hello():
print("Hello!")
say_hello()
# Output:
# Before function execution
# Hello!
# After function execution