Suppose you run a club with a hundred members. For each person you must store a name, an age and a position. If you keep them in lists, you soon end up asking questions like: was the age at position 1 or position 2 again? One slip, and a member’s age shows up as a job title.
Classes and objects are Python’s answer. A class is the plan, and an object is one real thing built from that plan. You write the plan once, with the name, age and position, and then create as many members as you need. Each one carries its own data, and each one knows how to do its own work, such as printing its details.
This tutorial explains classes and objects from the start. We create a class, build objects, use the __init__ method and the self parameter, add methods, and keep a count of members with a class variable. We also look at the mistakes beginners make most, with the exact error each one gives. Every example here was run, and the output shown is the real output.
Python is an object oriented language, which means that it organises code around objects. An object bundles two things: data, called attributes, and actions, called methods. A class is the template that tells Python what attributes and methods an object will have. One class can create any number of objects.
| Point | Class | Object |
|---|---|---|
| What it is | The plan or template | A real thing made from the plan |
| Example | Member | The member called Martin |
| How many | One | As many as you want |
| Created with | The class keyword | The class name followed by brackets |
Think of a club with hundreds of members. Each member has the same kinds of details, but different values. That is exactly the job of a class and its objects.
Write the keyword class, the name of the class, and a colon. Then write the body indented by four spaces. The usual style is to start a class name with a capital letter.
If you want an empty class for now, use the keyword pass, which means do nothing. A class body cannot be left blank.
Values written directly inside the class belong to the class, and every object can see them. Read them with the class name:
A common slip is to write the word class in place of the class name. Python cannot read that, and it stops with an error:
An object is also called an instance of the class. To create one, write the class name with brackets, and store it in a variable. A variable name cannot hold a space, so a line such as object name = Member() gives a SyntaxError. Use a single word, such as test.
The object test can read the club and estab values that belong to the class.
The class attributes above are the same for every object. Real objects need their own values, such as a different name for each member. The __init__ method does this. Python calls it automatically each time you create an object, so it runs once for every new object.
When you write Member with three values in brackets, Python makes a new empty object, and calls __init__ with that object as self, and the three values after it. The lines that start with self store the values on the object.
If you give too few values, Python tells you exactly which one is missing:
A method is a function that belongs to a class. Write it inside the class, with self as the first parameter. Keep a space between def and the method name.
Notice that we call m1.testfun() with no value in the brackets, although the method has one parameter. Python passes the object itself as self.
The word self is a reference to the object that is calling the method. It is how a method finds the data of its own object. When Roger calls testfun, self means Roger. When John calls it, self means John.
The name self can be changed. It does not have to be called self, but it must be the first parameter. This works:
Do not do this in real code. Every Python programmer expects the word self, and a different name makes your code harder to read. The example only shows that self is a convention, and not a keyword.
Now a full example. The class keeps a count of members with a class variable, which is a value shared by the whole class. It goes up by one each time an object is created. The three members are stored in a real list, so one loop can print them all.
The loop at the bottom is one of the biggest wins of using objects. You write the print code once, and it works for every member.
Each object has its own copy of its data. Changing one does not change the others.
Two objects with the same values are still two different objects, so a == b is False here. Python only calls two objects equal when you tell it how, or when they are the same object.
Python lets you add an attribute to one object after it is created, and delete it later. Use this with care, because other objects of the same class will not have it.
A value set inside __init__ with self belongs to that object, and is called an instance variable. A value written in the class body belongs to the class, and is called a class variable. An object can hide a class variable by setting a value of the same name on itself:
This is the mistake that surprises beginners most. If a class variable is a list, then every object shares that one list. Put the list inside __init__ so that each object gets its own.
In the Bad class, we added to b1, but b2 also shows the value, because they share one list.
If you print an object with no extra help, Python shows a text with the class name and a memory address, which is not useful. The address changes on every run, so it is not printed here. Define a __str__ method to choose what is shown.
Two extra kinds of methods are useful to know. A class method gets the class, and not the object, as its first parameter. A static method gets neither, and is just a function that sits inside the class for tidiness. Both use a decorator, a line that starts with the at sign.
| Function | What it does |
|---|---|
| type(obj) | Tells you which class the object came from |
| isinstance(obj, Class) | Checks if an object belongs to a class, or to a child of it |
| hasattr(obj, “name”) | Checks if an object has an attribute |
| vars(obj) | Shows the attributes of an object as a dictionary |
| dir(obj) | Lists all names that the object can use, including methods |
Compare a list and an object for the same member:
Both give the same answer, but rec_obj.position says what it means, and rec_list[2] makes you remember a number. With a hundred members and many fields, that difference matters. Objects can also carry their own methods, which lists cannot.
| Error | Usual cause | Fix |
|---|---|---|
| IndentationError | The class or method body is not indented | Indent by four spaces |
| SyntaxError on class.club | Using the word class to read an attribute | Use the class name, such as Member.club |
| SyntaxError on object name | A space inside a variable name | Use one word or an underscore |
| TypeError: missing required positional argument | Too few values when creating the object | Pass every value that __init__ needs |
| AttributeError: object has no attribute | The attribute was never set, or was deleted | Set it in __init__ |
| Curly quote SyntaxError | Code copied from a web page or a Word file | Type straight quotes in your editor |
Book class with a title, an author and a price. Create three books and print them in a loop.__str__ method so that print(book) shows the title and the author.Student. Add a list of marks to it, and make sure each student has their own list.Here is the full script that was used to test every example on this page. It ran on Python 3.13. A few lines use exec to show errors without stopping the script.
Once classes and objects click, the rest of object oriented Python becomes easier. Start by turning one real list of records in your own work, such as customers or products, into a class with a few methods. When that feels natural, move on to inheritance, and then to abstract classes and polymorphism.