9. Classes and Magic Methods

Python has class. In the world of programming languages, that means the ability to write user-defined types and give them abilities. A class is defined by what it does as much as what it contains. Most modern programming languages have this feature. Python adds a twist. It has something called magic methods, which are automatically called in response to certain circumstances.

Writing classes in Python is extremely simple at first. But it gets interesting quickly.

9.1 Classes and Objects: Basic Syntax

The basic syntax for writing a class in Python is shown here.

Image
class class_name:
statements

The statements consist ...

Get Supercharged Python: Take Your Code to the Next Level, First Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.