python

Object Oriented Programming


Object-oriented programming (OOP) is a programming paradigm that uses objects and classes to structure software programs. Python supports OOP, allowing developers to model real-world entities as classes and objects.

 

 

Classes and Objects in Python

In Python, classes and objects are fundamental building blocks of object-oriented programming (OOP). They allow you to create blueprints for data structures (classes) and instantiate those blueprints to create individual objects.

 

1. Classes

A class is a blueprint for creating objects. It defines a set of attributes and methods that the created objects will have.

Defining a Class:

class MyClass:
   # Class attribute
   class_attribute = "I am a class attribute"
   # Initializer method (constructor)
   def __init__(self, attribute1, attribute2):
       self.attribute1 = attribute1  # Instance attribute
       self.attribute2 = attribute2  # Instance attribute
   # Instance method
   def instance_method(self):
       return f"attribute1: {self.attribute1}, attribute2: {self.attribute2}"
   # Class method
   @classmethod
   def class_method(cls):
       return cls.class_attribute
   # Static method
   @staticmethod
   def static_method():
       return "I am a static method"
 

 

2. Objects

An object is an instance of a class. It represents a specific example of the class with actual values.

Creating an Object:

# Create an object (instance) of MyClass
my_object = MyClass("value1", "value2")
# Access instance attributes
print(my_object.attribute1)  # Output: value1
print(my_object.attribute2)  # Output: value2
# Call instance method
print(my_object.instance_method())  # Output: attribute1: value1, attribute2: value2
# Call class method
print(MyClass.class_method())  # Output: I am a class attribute
# Call static method
print(MyClass.static_method())  # Output: I am a static method
 

 

 

3. Attributes

3.1 Class Attributes:

Class attributes are shared by all instances of the class. They are defined within the class but outside any methods.

Example:

class Dog:
   species = "Canis familiaris"  # Class attribute
   def __init__(self, name, age):
       self.name = name  # Instance attribute
       self.age = age    # Instance attribute
# All Dog objects share the class attribute
dog1 = Dog("Buddy", 9)
dog2 = Dog("Lucy", 3)
print(dog1.species)  # Output: Canis familiaris
print(dog2.species)  # Output: Canis familiaris
# Changing the class attribute
Dog.species = "Canis lupus"
print(dog1.species)  # Output: Canis lupus
print(dog2.species)  # Output: Canis lupus
 

3.2 Instance Attributes:

Instance attributes are unique to each instance. They are defined within the __init__ method or other instance methods using self.

Example:

class Dog:
   def __init__(self, name, age):
       self.name = name  # Instance attribute
       self.age = age    # Instance attribute
dog1 = Dog("Buddy", 9)
dog2 = Dog("Lucy", 3)
# Each dog object has its own instance attributes
print(dog1.name)  # Output: Buddy
print(dog2.name)  # Output: Lucy
 

 

4. Methods

4.1 Instance Methods:

Instance methods operate on the instance of the class. They can access instance attributes and other instance methods using self.

Example:

class Dog:
   def __init__(self, name, age):
       self.name = name
       self.age = age
   def description(self):
       return f"{self.name} is {self.age} years old"
dog1 = Dog("Buddy", 9)
print(dog1.description())  # Output: Buddy is 9 years old
 

4.2 Class Methods:

Class methods are bound to the class, not the instance. They can access class attributes using cls.

Example:

class Dog:
   species = "Canis familiaris"
   @classmethod
   def get_species(cls):
       return cls.species
print(Dog.get_species())  # Output: Canis familiaris
 

4.3 Static Methods:

Static methods do not operate on an instance or the class. They are defined using the @staticmethod decorator.

Example:

class Dog:
   @staticmethod
   def bark():
       return "Woof!"
print(Dog.bark())  # Output: Woof!
 

 


python