Lesson 21 of 30

In progress

Course progress

Python

Classes and objects: data with behaviour

Write a class, create independent objects from it, and see why bundling data with behaviour beats passing dictionaries around.

A dictionary can hold a student’s name and score, but it cannot stop you setting the score to "excellent". A class can. That is the difference between storing data and modelling a thing.

What you will learn

  • Define a class and create objects from it.
  • Give objects their own attributes and methods.
  • Explain why a class beats a loose dictionary once behaviour appears.

A class is a blueprint

A class describes what every object of that kind has and can do. An object is one concrete instance made from the blueprint.

Code
Code runs on the Java or .NET runtime, not in a browser tab. Copy this example into your editor and run it there.



Press Run to see the output.

class Student: defines the blueprint. Student("Ana", 88) creates one object and runs __init__ to fill in its attributes. self refers to the object being created, so self.name = name means "store this value on this object".

Each object has its own attributes. Changing ana.score does not touch ravi.score. That independence is the reason two objects from one class behave like two separate records.

Attributes are values, methods are actions

Attributes are reached with a dot: ana.name. Methods are functions defined in the class body and reached the same way: ana.is_passing(). The brackets matter; ana.is_passing without them refers to the method object and prints something like a bound method, which is rarely what you want.

A method can read and change the object’s own state:

Code
Code runs on the Java or .NET runtime, not in a browser tab. Copy this example into your editor and run it there.



Press Run to see the output.

Counter() takes no arguments beyond the implicit self, because __init__ only declares self. That is valid and common for objects that start with a fixed default.

Why not just use a dictionary?

For pure storage, a dictionary is simpler. The class earns its place when the data has rules.

Code
Code runs on the Java or .NET runtime, not in a browser tab. Copy this example into your editor and run it there.



Press Run to see the output.

Now every change goes through one place that knows the rule. A dictionary would happily accept 140, and the bad value would surface later in an average or a report, far from the code that caused it.

Objects can hold other objects, which is how real models are built: an Order with a list of order lines, a Course with a list of lessons. Each class keeps its own rules and the parent object coordinates them, instead of one enormous class knowing everything. That structure also makes each part testable on its own, because a class can be exercised without building the whole order around it.

The same idea applies to derived values. is_passing is computed from the score, so it cannot drift out of sync the way a separately stored passing flag can.

Objects print unhelpfully by default

Code
Code runs on the Java or .NET runtime, not in a browser tab. Copy this example into your editor and run it there.



Press Run to see the output.

The output looks like <__main__.Student object at 0x...>, which tells you the class and the memory address and nothing useful. Lesson 22 fixes it with __str__. For now, print attributes explicitly.

Forgetting self in a method definition is the classic first error. def is_passing(): inside a class produces TypeError: is_passing() takes 0 positional arguments but 1 was given, because Python passes the object automatically. Every instance method needs self as its first parameter, even if the body does not use it.

Write a Book class with a title and a page count. Add a method is_long() that returns True above 300 pages, and a method describe() that returns a sentence with both values. Create two books and print their descriptions.

Show solution
Code
Code runs on the Java or .NET runtime, not in a browser tab. Copy this example into your editor and run it there.



Press Run to see the output.

describe returns a string rather than printing, so the caller decides where it goes. That keeps the class usable in a report, a test or a page template.

Recap

  • A class is a blueprint; an object is one instance with its own attributes.
  • Methods are functions on the class that receive the object as self.
  • Use a class when data has rules or behaviour, not merely because it has several fields.

Next, you will look closely at __init__ and self: what runs at creation, and how one shared attribute can leak between every object.

Do I need a class for everything?
No. Functions and dictionaries handle a lot of work with less ceremony. Reach for a class when several functions operate on the same data, or when the data needs validation that must not be bypassed.
What does the object address in the default print mean?
It is where the object lives in memory for this run. It changes between runs, so it is useless for output. Define __str__ when objects need to be printed.

Progress is saved in this browser only. No account, no email.

Admin

About the author

Admin

Instructor and technical writer at tutorials.in

Writes beginner lessons for tutorials.in. Each lesson is checked against the language or tool documentation, and examples are run before publishing.

Reviewed by the editorial team before publishing. Last updated November 16, 2026.