Open In App

Python objects

Last Updated : 04 Oct, 2025
Suggest changes
Share
Like Article
Like
Report

A class is like a blueprint or template for creating objects. It defines what attributes (data) and methods (functions) the objects will have. In Python, everything is an object - from numbers and strings to lists and user-defined classes. An object combines data (attributes) and behavior (methods) into a single unit

For example:

  • Suppose we create a class called Dog with attributes like breed, age, and color, and behaviors like bark(), sleep(), and eat().
  • An object of this class could be: a Labrador that is 5 years old and black in color.

You can create as many dog objects as you like, each with different values, but all of them follow the same Dog class.

Key Features of an Object

Every object in Python has three important characteristics:

  • State: Represented by attributes (e.g., age, breed, color).
  • Behavior: Represented by methods (e.g., bark(), sleep(), eat()).
  • Identity: A unique identity that distinguishes one object from another.
python_class-objects
class and object representation

Creating (Instantiating) Objects

When you create an object from a class, it is called instantiation.

  • All instances of a class share the same structure (attributes and methods).
  • However, the actual values of attributes are unique for each object.

Example:python-objects Example: 

Python
class Dog: def __init__(self, breed, age, color): self.breed = breed self.age = age self.color = color # Behaviors def bark(self): print(f"{self.breed} says: Woof! Woof!") def sleep(self): print(f"{self.breed} is sleeping... Zzz") def eat(self, food): print(f"{self.breed} is eating {food}") # Creating dog objects dog1 = Dog("Labrador", 5, "Black") dog2 = Dog("Beagle", 3, "Brown") # Accessing attributes print(dog1.breed) print(dog2.age) # Using behaviors dog1.bark() dog2.sleep() dog1.eat("bone") 

Output
Labrador 3 Labrador says: Woof! Woof! Beagle is sleeping... Zzz Labrador is eating bone 

Explanation:

  • Class (Dog): blueprint defining attributes (breed, age, color) and behaviors (bark, sleep, eat).
  • Attributes: store object state; each dog has its own values (e.g., dog1.breed = "Labrador").
  • Methods: define actions a dog can perform (bark(), sleep(), eat(food)).
  • Objects (Instances): dog1 and dog2 are specific dogs with their own state and behaviors.
  • Encapsulation: attributes and methods are bundled, so each object manages its own state and actions.

Article Tags :

Explore