Object Orientated Programming

Python is an Object Orientated Programming (OOP) language. In OOP each instance (right) belongs to a class (left).

The class can be conceptualised as a blueprint which gives instructions on constructing an instance and contains methods which can be used by each instance independently.

The class name is called MobileSuit. PascalCase is typically used for third-party class names.

The instance names green_instance_1, green_instance_2, green_instance_3, green_instance_4, green_instance_5, green_instance_6, blue_instance_7, blue_instance_8, purple_instance_9, purple_instance_10 and purple_instance_11. snake_case is typically used for instance names.

Notice that the class name and instance name are depicted as labels. These labels are typically used to select an instance, so that an attribute can be read off an instance or a method can be called from an instance.

Instantiation Signature

Each class has an initialisation signature. The docstring of the initialisation signature can be viewed as a popup balloon by inputting the class name with open parenthesis. Some IDEs such as JupyterLab require the additional keyboard shortcut shift ⇧ and tab ↹ for the docstring popup balloon to show.

Alternatively to view the docstring output in an IPython cell use ?

? MobileSuit
Init signature:  MobileSuit(color='green', equipment=None, /)
Initialise a new mobile suit with the specified color and optional equipment.

The purpose of the initialisation signature is to provide the necessary instance data required when the instance is constructed.

There are two input arguments color and equipment. These have the default values 'green' and None which will be used by default when these are not specified.

The / indicates that all the input arguments must be supplied positionally.

A new instance can be created by using:


Under the hood, a __new__ data model method is used which constructs a new instance. __new__ calls the __init__ data model method which is used to supply the instance, with instance data. In this case specifying the paint color to be 'green' and the equipment to be None.

Notice that this instance has no instance name or label. An instance name or label is used to reference an instance. An instance that has no references cannot be interacted with and is orphaned. Orphaned instances are cleaned up automatically by Python's garbage collection.

The instance name is assigned to an object using the assignment operator =

green_instance = MobileSuit()

A second instance can be created using:

blue_instance = MobileSuit(color='blue')

And a third instance can be created using:

purple_instance = MobileSuit(color='purple')


An attribute is a property that can be referenced from an instance using dot notation. For example the attribute color will return the color string used for each instance:


And the attribute equipment will return the equipment each instance has:


This is an empty dictionary as green_instance has no equipment.

Class Attribute

If the three instances are examined, a number of similarities can be observed. All three instances have the same height 16.2 m.

The height is constant for each instance and is therefore provided as a class attribute. The class attribute can be accessed from the class:


And can also be accessed from each instance:



A method is a function that is defined in the class and can be used by each instance of a class:

For example the method raise_right_arm can be referenced from the class using:


Conceptualise calling a function as invoking an action. To call a function, parenthesis must be used. The parenthesis are also used to supply any input arguments. For a method to be called an instance must be supplied which gives the method instance data to work with:


Within the class, self is a generic placeholder which denotes this instance. In the above self becomes green_instance.

Notice that only the green instance has its right arm raised and calling the method on green_instance does not change blue_instance or purple_instance.

A method is usually called directly from an instance:


Because the method is being called from self which recall means this instance and in this case is green_instance, the instance data is supplied. This method raise_right_arm only requires instance data as the right arm is internal to the instance.

Other methods may require additional input arguments to provide external (to the instance) information. For example, the method mount_equipment_left_shoulder will require an instance as well as an external piece of equipment to be mounted on the left arm:

For example an external shield can be mounted on the left shoulder of the green instance using:


This can also be called from a class by providing an instance and the external piece of equipment:

MobileSuit.mount_equipment_left_shoulder(green_instance, 'shield')

These methods are known as instance methods as they require an instance data. Instance methods are the most commonly used methods and therefore usually just referred to as methods.

Class Method

A class method is a method that is bound to a class and not an instance. The most common purpose for a class method is an alternative constructor which returns an instance. For example the land_mode alterative constructor can be used to construct a MobileSuit instance with a color and equipment set for land and the space_mode alternative constructor can be used to construct a MobileSuit instance commonly configured for space mode.


These alternative constructors can be used to construct the following 5 instances which have the default color and equipment for each terrain:

land_1 = MobileSuit.land()
space_2 = MobileSuit.space()
land_3 = MobileSuit.land()
space_4 = MobileSuit.space()
space_5 = MobileSuit.space()

And the attributes for some of these instances can be examined:

{'left_shoulder': 'shield', 'right_hand': '105 mm rifle'}
{'back': 'space backpack', 'right_hand': 'beam rifle'}

It is possible to call a class method from an instance. For example the instance land_1 can be created from the class MobileSuit using the class method land and then the four other MobileSuit instances can be created by calling the space or land class methods from the instance land_1:

land_1 = MobileSuit.land()
space_2 = land_1.space() #land_1 is an instance of MobileSuit
land_3 = land_1.land() #land_1 is an instance of MobileSuit
space_4 = land_1.space() #land_1 is an instance of MobileSuit
space_5 = land_1.space() #land_1 is an instance of MobileSuit

This works because land_1 is an instance of the class MobileSuit and therefore the class is inferred from the instance land_1.

Static Method

A static method is a method that is neither bound to the class or the instance. The purpose of a static method is to insert a function into the classes namespace because it may be expected to be found there. For example the method possible_equipment can be used to return a list of possible equipment in response to a location.

This can be called from the class:

[None, 'parachute', 'flight pack', 'space pack']
[None, 'cannon', 'shield', 'dobbergun']
[None, '105 mm rifle', 'beam rifle', 'beam sabre']

Or from an instance:

[None, 'parachute', 'flight pack', 'space pack']