Module 11 · Object-Oriented Programming — topic 2 of 25
Classes
Defining a class with the class statement, naming conventions, the class body, class objects versus instances, and inspecting classes with type() and isinstance().
Learning objectives
- Write a class definition with methods
- Create instances by calling the class
- Use type() and isinstance() with your own classes
Definition
A class is a user-defined type created with the class statement. Its body defines attributes and methods shared by all instances; calling the class like a function creates a new instance.
Introduction
A class statement creates a new type, just as str and list are types. After class Book: ..., you can create books with Book() and check them with isinstance(x, Book).
Why it matters
Designing your own types lets your code speak the language of the problem: Order, Invoice, Player, Room — instead of anonymous dictionaries.
Detailed explanation
Syntax: class Name: followed by an indented body. Class names use PascalCase (BankAccount).
The body usually contains method definitions (def inside the class) and sometimes class-level variables. pass can be used for an empty class.
Running the class statement creates a class object bound to the name. Calling it — Book() — creates an instance and runs __init__ if defined.
type(obj) returns an object's class; isinstance(obj, Class) checks whether obj is an instance of the class (or a subclass).
Every instance has its own namespace for data (stored in obj.__dict__), while methods live on the class and are shared.
Syntax
- Class names use PascalCase.
- Methods are defs inside the class.
- Calling the class creates an instance.
Code examples
A minimal class
Empty True attributes can be added
A class with methods
Clean Code by Robert Martin A book in a library.
More examples
Classes as types
Python Basics short
Algorithms long
skipping str
{'title': 'Python Basics', 'pages': 220}
TrueStep-by-step explanation
- isinstance filters the mixed list so only Book objects are asked for is_long().
- vars(obj) shows the instance's own attributes as a dictionary.
- Book.is_long(shelf[1]) calls the method through the class, passing the instance explicitly — exactly what shelf[1].is_long() does behind the scenes.
Expected output
Running “Classes as types” prints exactly:
Practical applications
- Domain models: Customer, Order, Product.
- Data containers for API responses.
- Custom exceptions are classes too.
Best practices
- Use PascalCase and a docstring for every class.
- Define attributes in __init__, not by adding them later from outside.
- Prefer isinstance over comparing type() directly.
Common mistakes
Forgetting parentheses when creating an instance
Avoid
Prefer
Without parentheses you refer to the class, not a new instance.
lowercase class names
Avoid
Prefer
PEP 8 uses PascalCase for classes so they are easy to tell apart from functions.
Important notes
- Classes are themselves objects (instances of type), which is why you can pass them around and inspect them.
Key takeaways
- class Name: creates a new type.
- Calling the class creates an instance.
- type() and isinstance() work with your classes.
Try it yourself
- 1Define a Rectangle class with width, height and area() and perimeter() methods.
- 2Create a list of three rectangles and print the largest by area.
Finished reading? Mark it complete to update your dashboard.
Suggested practice
1 question test this exact topic. Answers and explanations are revealed after you submit.
- ModerateWhat is printed?