Monday , November 30 2026
Python classes and objects: attributes, methods, __init__, self, class vs instance attributes and __str__, with tested examples.

Python Classes and Objects with Examples

Updated: 29 November 2026Level: BeginnerReading time: about 9 minutesPython 3 · tested

A class is a blueprint that describes what data an object holds and what it can do. An object (instance) is one concrete thing made from that blueprint. For example, Student is a class; Riya and Aman are objects.

What You Will Learn

  • Define a class with attributes and methods
  • The __init__ constructor and self
  • Class attributes vs instance attributes
  • The __str__ method for readable printing
  • isinstance() and type()

Prerequisites

Python Functions.

Define a Class and Create Objects

Example
class Student:
    school = "Itronix Academy"          # class attribute (shared)

    def __init__(self, name, marks):
        self.name = name                # instance attributes
        self.marks = marks

    def grade(self):
        return "A" if self.marks >= 90 else "B" if self.marks >= 75 else "C"

    def __str__(self):
        return f"{self.name} ({self.marks})"

s1 = Student("Riya", 91)
s2 = Student("Aman", 78)
print(s1, s1.grade())
print(s2, s2.grade())
print(s1.school, Student.school)
print(isinstance(s1, Student), type(s2).__name__)
Output
Riya (91) A
Aman (78) B
Itronix Academy Itronix Academy
True Student
Part Meaning
class Student: Defines the blueprint
__init__ Constructor – runs automatically when an object is created
self The object itself; Python passes it automatically
self.name Instance attribute – each object has its own
school Class attribute – shared by all objects
grade() Method – a function that belongs to the class
__str__ Defines what print(obj) shows

Explanation: Student("Riya", 91) calls __init__ with self = the new object. Each student keeps its own name and marks, but they share school.

Changing Attributes

Example
s2.marks = 95
print(s2.grade())
Student.school = "Itronix Solutions"
print(s1.school)
Output
A
Itronix Solutions

Explanation: Changing an instance attribute affects one object; changing a class attribute through the class affects all objects that have not overridden it.

Series

Python series: What is Python · Installation · Syntax · Variables · Operators · Decisions · Loops · Strings · Lists · Dictionary · Functions · Lambda · Modules · Files · Exceptions · OOP · Classes & Objects · Inheritance · Decorators · Generators · Virtual Environments

Interview Questions

Q: What is self?
A reference to the current instance, passed automatically as the first argument of instance methods.

Q: Class vs instance attribute?
A class attribute is shared by all instances; an instance attribute belongs to one object.

Q: What is __init__?
The initializer (constructor) method called when an object is created.

Practice Exercises

  1. Create a Book class with title, author and price, and a method that applies a discount.
  2. Add a __str__ method that prints “Title by Author – Rs price”.

Summary

Classes bundle data (attributes) and behaviour (methods); __init__ sets up each object, self refers to the object, and __str__ controls printing.

Next: Inheritance · OOP Concepts

About Machine Learning

Check Also

Python Inheritance: super(), Method Overriding and MRO

Python inheritance with super(), method overriding, isinstance, multiple inheritance and the method resolution order.