What is an object in Python

In Python, an object is an instance of a class. Everything in Python—whether it’s a number, a string, a list, a dictionary, or even a function—is an object.

What does an Object do in Python?

An object in Python is a container for holding both data (attributes) and behavior (methods). This is one of the basic constructs of Python programming.

Think of an object as a real-life entity. A car is an example of an object. It possesses:

  • Attributes (Data): Just like its color, model, and brand.
  • Behaviors (Methods): Such as acceleration, braking, and turning.

We define objects in Python using classes that serve as templates for the creation of objects.

Key Characteristics of Objects in Python

  1. Encapsulation:
  • Objects combine data (attributes) and behavior (methods) into a single entity.
  • Example:
class Car:
    def __init__(self, brand, model, color):
        self.brand = brand
        self.model = model
        self.color = color
    
    def drive(self):
        return f"The {self.color} {self.brand} {self.model} is driving."
    
# Create an object (instance) of the Car class
my_car = Car("Toyota", "Corolla", "Red")

# Access attributes and methods
print(my_car.brand)      # Outputs: Toyota
print(my_car.drive())    # Outputs: The Red Toyota Corolla is driving.

2. Identity:

  • Every object has a unique identity (like a memory address). This can be checked with the id() function.
  • Example:
obj1 = [1, 2, 3]
obj2 = [1, 2, 3]

print(id(obj1))  # Unique identifier for obj1
print(id(obj2))  # Unique identifier for obj2 (different from obj1)

3. Type:

  • Objects have types that define their behavior. The type() function tells you the type of an object.
  • Example:
print(type(5))         # Outputs: <class 'int'>
print(type("Hello"))   # Outputs: <class 'str'>
print(type([1, 2, 3])) # Outputs: <class 'list'>

4. Mutability:

  • Some objects can be changed after they are created (mutable), while others cannot (immutable).
  • Examples:
    • Mutable: list, dict, set
    • Immutable: int, str, tuple
  • Example:
# Mutable object (list)
my_list = [1, 2, 3]
my_list.append(4)
print(my_list)  # Outputs: [1, 2, 3, 4]

# Immutable object (string)
my_string = "Jobbinge"
new_string = my_string.upper()  # Creates a new string
print(new_string)  # Outputs: JobbingeReal-World Analogy

print(my_string)   # Outputs: Jobbinge (unchanged)

Components of an Object

  1. Attributes (Data):
  • Attributes store information about the object. They are like variables that belong to an object.
  • Example:
class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age

person1 = Person("Alice", 30)
print(person1.name)  # Outputs: Alice
print(person1.age)   # Outputs: 30

2. Methods (Behaviors):

  • Methods define the behavior of an object. They are functions that belong to a class and operate on its attributes.
  • Example:
class Dog:
    def __init__(self, name):
        self.name = name
    
    def bark(self):
        return f"{self.name} says Woof!"

dog1 = Dog("Buddy")
print(dog1.bark())  # Outputs: Buddy says Woof!

How to Create and Use Objects

  1. Define a Class (Blueprint):
  • A class is a template that describes the attributes and methods an object will have.
  • Example:
class Phone:
    def __init__(self, brand, model):
        self.brand = brand
        self.model = model
    
    def call(self, number):
        return f"Calling {number} from {self.brand} {self.model}."

2. Create an Object (Instance):

  • Use the class to create an object.
  • Example:
my_phone = Phone("Apple", "iPhone 14")
print(my_phone.brand)  # Outputs: Apple
print(my_phone.call("123-456-7890"))  # Outputs: Calling 123-456-7890 from Apple iPhone 14.

Real-World Analogy

Think of a class as a blueprint for building objects.

  • Blueprint: A house design plan (class).
  • Object: A house built from that blueprint.
  • Attributes: Number of rooms, color of walls, size of the garden (data about the house).
  • Methods: Actions like opening a door, switching on the lights (behavior of the house).

For example, if a class represents a blueprint for a Car, then:

  • Any object created from that class, say a Toyota Corolla or a Honda Civic, is an instance of that class.
  • The cars can have varying attributes, such as brand, model, or color; however, they all share common behaviors, like drive and brake methods.

Why objects are important:

  • Modularity: Objects organize data and behavior into reusable and independent components.
  • Real-World Modeling: Objects enable you to model real-world entities in your code.
  • Reusability: A class can be reused to provide several objects which perform similar work.
  • Flexibility: Objects make modification and extension easier without affecting any other part of the code.