Classes

A class is a complex type, that is, not a primitive like an integer. It has attributes and methods. An attribute is a variable that belongs to a class. A method is a function coupled to the class. Such function-object coupling is the philosophy of object-oriented programming and is a form of compartmentalization.

Classes are defined with the keyword class and a list of methods. Methods that start and end with double underscores __ are reserved names and are called implicitly. The method __init__ is the most common, and it is called when an instance is made such as to initialize attributes.

class Message:

    def __init__(self, text, id):
        self.text = text
        self.id = id

    def print(self):
        print(self.text)

msg = Message("Hello, World!", 1) # Initialize
msg.id # Access an attribute
msg.print() # Call a method

The variable self refers to the class itself. Every method should start with self as the first argument.