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 O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.