Python informer

Improve your Python coding skills

Example classes

To illustrate magic methods, we will use a couple of simple user defined classes as examples. These will be extended by adding magic methods in the remaining articles.

The Person class

Here is a simple Person class:

class Person:

    def __init__(self, title, forename, surname):
        self.title = title
        self.forename = forename
        self.surname = surname

This class simply holds the person’s title, forename and surname as string fields.

The Matrix class

We will define a class that holds the values for a 2 by 2 matrix

$$\begin{pmatrix}a & b\\c & d\end{pmatrix}$$

Here is the simple Matrix class:

class Matrix:

    def __init__(self, a, b, c, d): = [a, b, c, d]