Classes In Python

Python Modules

In Python, a module is simply a file with the .py extension that contains Python code. Python modules are imported into other Python modules using the import or from keywords. Importing modules makes the objects in those modules accessible to the module doing the import.

Python Classes

A class is a container for other Python objects such as variables and functions. Everything inside the class must be indented as it is part of the class "block."

The Python self Parameter

The self parameter is used in an instance method to gain access to the object (the instance) that called the method.

Python Instance Methods

Instance methods are functions defined in a class that can refer to values stored in instance variables, as well as having access to class variables.

Python Class Methods

Class methods are functions defined in a class that refer to the class as a whole rather than an individual instance of a class.

Python Static Methods

Static methods are functions defined in a class that do not have access to the class object or any instance of that class. Static methods are called using the class, not an instance of the class.

Python __init__ method

The __init__ method, if defined in a class, is called by Python whenever a class is instantiated. This method allows the class to be initialised and setup with whatever initial state is needed.

Creating and initialising a Class in Python

To create an instance of a class ("to instantiate it") do

Python Attributes

Python classes are simply a bag of named attributes. An attribute being anything that follows a "." using the Python syntax. eg. Myclass.variable, MyClass.method(), MyClass.__doc__.

Python Class Instance Variables

Variables that a class defines using self can only be accessed through an instance of the class. Instance variables are created in the constructor (__init__ method) for the class.

Python Class Variables

A Class variable in Python is a variable that is accessible via the class rather than via an individual instance of the class. It's a variable shared among the class as a whole. Other languages sometimes refer to these as static variables or static fields.

Python Class Properties

Properties are setter/getter methods that set/get the value of a variable contained in the class.

Python Private Attributes and Functions

In many programming languages, there is the concept of "private" class attributes - variables and functions that cannot be accessed by anyone outside of the class. In Python, all variables and functions defined by a class are always public. Therefore, the convention is to use an underscore before the variable or function name to indicate that it is private.

Python Inheritance

A class can inherit the attributes of a super-class. This is one of the features of object-oriented programming, where a more specialized class can be defined that uses the attributes (functions and variables) of a super-class and extends the behavior of the super-class. By using a super-class, you can avoid repeating common attributes of "like" classes but also add or override behaviors of the super-class. It also common to use the term "derived" - in the example below, Student and Teacher are derived from Person.

The Python cls Parameter

The cls parameter is used in a class method to gain access to the class to which the method belongs.