Classes
Python - A Quick Start for existing Programers
3 min read
Published Sep 16 2025, updated Sep 30 2025
Guide Sections
Guide Comments
Python supports OOP and classes:
- Defined by the
classkeyword. - Structure is indented.
- Top level variables are class attributes available on all instances of a class.
- Constructer is defined as a function with the name
__init__(self, ...). - Self object is used to set instance variables:
self.my_instance_variable = 4. - Constructer parameters can be passed in to the init function:
__init__(self, my_parameter1). - Inside the init function set
self.my_instance_variable = my_parameter1to set instance variables.
- Self object is used to set instance variables:
- Top level functions are instance functions by default and can access
self.
Example:
Class methods
Instance Method (default):
- Normal methods in a class.
- Take
selfas the first argument. - Can access instance attributes and class attributes.
- Is called on an individual instance of a class.
Class Method (@classmethod):
- Declared with
@classmethoddecorator. - First parameter is
cls(the class itself, not an instance). - Can access/modify class attributes but not instance attributes (unless given an instance).
- Often used as factory methods (alternate constructors).
- Is called on the actual class record.
Static Method (@staticmethod):
- Declared with
@staticmethoddecorator. - No
selforclsparameter → acts like a regular function, but lives inside the class for organisation. - Cannot access class or instance attributes (unless passed explicitly).
- Used for utility/helper functions related to the class.
- Is called on the actual class record.
Dunder methods:
- Special methods denoted with
____either side. - Has special purposes, such as the
__init__function. - See section below for more details.
Encapsulation (Attribute Access)
There is no true private attribute enforcement like in other languages, Instead a naming convention using _ and __ is used.
Public (no underscore):
- Normal attributes are fully accessible.
Protected (_single):
- Just a convention: “for internal use, but you can access it if you really want.”
Private (__double):
- Triggers name mangling: Python renames it to
_ClassName__attr. - Makes accidental access harder, but still accessible if you know the mangled name.
Inheritance
Inheritance is supported in Python classes, allowing you to use inherited attributes and methods and override methods.
The super() function
- Calls methods of the parent class.
- Commonly used in
__init__to ensure parent initialisation runs, see highlighted row in the example below where it calls the parents constructor first to initialise the common attributes and then sets the class specific attributes. - Avoids hardcoding the parent class name → important in multiple inheritance.
Example: Calling parent methods:
Multiple Inheritance and super()
Python supports multiple inheritance. super() follows the MRO (Method Resolution Order).
Dunder methods
Python has a lot of dunder (double underscore) methods, also called magic methods. They let your objects integrate seamlessly with Python’s built-in syntax and functions.
- Dunder methods = special hooks that let your class behave like built-in types.
- This allows you to be able to dfine how you class works with the
lenmethod, or how it will print when doing anf stingetc. - Categories:
- Initialisation (
__init__,__str__,__repr__) - Attribute access (
__getattr__,__setattr__) - Callable/context (
__call__,__enter__,__exit__) - Comparisons (
__eq__,__lt__, etc.) - Containers (
__len__,__getitem__, etc.) - Operators (arithmetic, bitwise, in-place, reflected)
- Misc (
__bool__,__index__, async methods)
- Initialisation (
Examples:














