GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Python Tutorial
  • Data Types
  • Interview Questions
  • Examples
  • Quizzes
  • DSA Python
  • Data Science
  • NumPy
  • Pandas
  • Practice
  • Django
  • Flask
  • self
  • Introduction1 min read

This view

  • Original page
  • New address

GeezerVison is showing www.geeksforgeeks.org — Python Classes and Objects - GeeksforGeeks

geeksforgeeks
search icon
  • Courses
  • Tutorials
  • Interview Prep
  • Python Tutorial
  • Data Types
  • Interview Questions
  • Examples
  • Quizzes
  • DSA Python
  • Data Science
  • NumPy
  • Pandas
  • Practice
  • Django
  • Flask

Python Classes and Objects

Last Updated : 19 Sep, 2026

A class is a user-defined template for creating objects. It bundles data and functions together, making it easier to manage and use them. When we create a new class, we define a new type of object. We can then create multiple instances of this object type.

420046617
Classes and Objects (Here Dog is the Base Class and Bobby is Object)

Creating a Class

Classes are defined using the class keyword, followed by the class name and a colon. Attributes are variables defined inside class and represent properties of the class. Attributes can be accessed using dot (.) operator (e.g., MyClass.my_attribute).

Python
# define a class
class Dog:
    sound = "bark"  # class attribute

Object

An object is a specific instance of a class. It holds its own set of data (instance variables) and can invoke methods defined by its class. Multiple objects can be created from same class, each with its own unique attributes.

Let's create an object from Dog class.

Python
class Dog:
    sound = "bark"

# Creating object from class
dog1 = Dog()  
print(dog1.sound) 

Output
bark

Explanation: sound attribute is a class attribute. It is shared across all instances of Dog class, so can be directly accessed through instance dog1.

Initiate Object with __init__()

__init__() method acts as a constructor and is automatically executed when an object is created. It is used to initialize the attributes of the object with the values provided at the time of object creation.

Python
class Dog:
    species = "Canine"  # Class attribute

    def __init__(self, name, age):
        self.name = name  # Instance attribute
        self.age = age  # Instance attribute

# Creating an object of the Dog class
dog1 = Dog("Buddy", 3)

print(dog1.name)  
print(dog1.species)  

Output
Buddy
Canine

Explanation:

  • self refers to the current object and is used to store data inside it.
  • Dog("Buddy", 3) creates an object and passes values to __init__().
  • self.name and self.age store the object’s name and age.
  • dog1.name accesses the object’s instance attribute and dog1.species accesses the shared class attribute.

__str__() Method

__str__() method allows us to define a custom string representation of an object. By default, when we print an object or convert it to a string using str(), Python uses the default implementation, which returns a string like <__main__.ClassName object at 0x00000123>.

Take an example of using __str__() method to provide a readable string output for an object:

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

    def __str__(self):
        return f"{self.name} is {self.age} years old."
dog1 = Dog("Buddy", 3)
dog2 = Dog("Charlie", 5)

print(dog1)  
print(dog2)  

Output
Buddy is 3 years old.
Charlie is 5 years old.

Explanation:

  • __str__() Implementation: Defined as a method in Dog class. Uses self parameter to access instance's attributes (name and age).
  • Readable Output: When print(dog1) is called, Python automatically uses __str__() method to get a string representation of object. Without __str__(), calling print(dog1) would produce something like <__main__.Dog object at 0x00000123>.

To know about Class and Instance variable, refer to this article Class (Static) and Instance Variables in Python.

Difference Between Class and Object

ClassObject
Blueprint for creating objectsInstance of a class
Defines attributes and methodsUses attributes and methods
Created using class keywordCreated by calling the class
Comment

Explore

    Python Fundamentals
    • Introduction1 min read
    • Input & Output2 min read
    • Variables4 min read
    • Operators4 min read
    • Keywords2 min read
    • Data Types4 min read
    • Conditional Statements3 min read
    • Loops3 min read
    • Functions4 min read
    Python Data Structures
    • String4 min read
    • List3 min read
    • Tuples3 min read
    • Dictionary3 min read
    • Set2 min read
    • Arrays5 min read
    Advanced Python
    • OOP Concepts3 min read
    • Exception Handling5 min read
    • File Handling4 min read
    • Python Database2 min read
    • MongoDB3 min read
    • MySQL7 min read
    • Packages4 min read
    • Modules3 min read
    • DSA Libraries5 min read
    • Python GUI2 min read
    Data Science with Python
    • Numpy3 min read
    • Pandas4 min read
    • Matplotlib3 min read
    • Seaborn3 min read
    • StatsModel2 min read
    • Model Building6 min read
    • TensorFlow2 min read
    • PyTorch5 min read
    Web Development with Python
    • Flask3 min read
    • Django5 min read
    • Django ORM4 min read
    • Jinja2 Templating5 min read
    • Django Templates5 min read
    • REST API3 min read
    • Build API with DRF4 min read
    Python Practice
    • Quiz1 min read
    • Practice Problems2 min read
    • Interview Q & A15+ min read
    Courses
    • Placement 360 Course2 min read
    • Data Science 360 Course2 min read
    • AI Engg Course2 min read
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.