Asset Classes

Free investment financial education

Language

Multilingual content from IBKR

Close Navigation
Learn more about IBKR accounts
Object Oriented Programming (OOP) in Python – Part V

Object Oriented Programming (OOP) in Python – Part V

Posted May 5, 2021 at 11:30 am

Jay Parmar
QuantInsti

Review Part I,  Part II , Part III and Part IV for an overview of Python classes and their objects.

What are the attributes and methods?

I am pretty sure that you know what I mean by attributes and methods. We have seen examples of attributes and methods multiple times by now. Keeping this in mind, I will directly jump to its implementation in Python.

The below example shows how to define a class with some default attributes and methods:

class Car:

colour = ‘White’
seating_capacity = 5

def drive_forward(self, meters):
print(f’Driving {meters} meters ahead’)

def lower_windows(self):
print(‘Lowering windows on all doors’)
print(‘Windows lowered’)

The updated class definition now resembles a real-world car to some extent. This time it has got two attributes, colour and seating_capacity, and two methods, drive_forward() and lower_windows() built-in it. That means, when we create an object of such a class, it will have these attributes and methods by default.

Of course, we can update these attribute values (neither all cars are White in colour nor all cars have a seating capacity of five). A new car object created below using the new class defined demonstrates this point.

car_3 = Car()

# Printing the default values
print(‘The colour of Car 3 is:’, car_3.colour)
print(‘The seating capacity of Car 3 is:’, car_3.seating_capacity)

The output is shown below:

The colour of Car 3 is: White
The seating capacity of Car 3 is: 5

As we can see in the above example, we can access a given object’s attributes (and methods) using the dot operator. To modify default behaviour, we simply assign new values to attributes as shown below.

car_3.colour = ‘Magma Red’
car_3.seating_capacity =
# Printing the modified values
print(‘The colour of Car 3 is:’, car_3.colour)
print(‘The seating capacity of Car 3 is:’, car_3.seating_capacity)

It will yield the following result:

The colour of Car 3 is: Magma Red
The seating capacity of Car 3 is: 2

In the real-world analogy, this operation is similar to modifying a car in real. The methods within a class define functionalities of a car. This means we can call methods using the objects only. Because if we don’t have a car, there won’t be a question of accessing its functionality.

We call methods on car_3, as shown below:

# Commanding the car to drive 500 meters
car_3.drive_forward(500)

# Commanding the car to lower windows
car_3.lower_windows()

Calling methods, as shown above, would output the following:

Driving 500 meters ahead
Lowering windows on all doors
Windows lowered

One thing to note here is, we cannot alter the behaviour of the methods defined within a class using their objects.

In this fashion, we can create as many objects of the class Car as we need. But wait, let’s consider that we create twenty objects.

In this case, the colour attribute of all those objects would have the same value, White. And we all know that in the real world, we have cars with all imaginable colours.

To replicate such a scenario, we might want to change the colour of those twenty objects. In the current implementation, we would need to change the colour attributes of all those objects. This approach does not seem to be efficient.

Instead, what if we can have a facility to change each object’s colour the moment we create them__init__ method to our rescue.

Stay tuned for the next installment in which Jay will discuss the init method.

Visit QuantInsti to read more about this research: https://blog.quantinsti.com/object-oriented-programming-python/.

Disclosure: Interactive Brokers

Information posted on IBKR Campus that is provided by third-parties does NOT constitute a recommendation that you should contract for the services of that third party. Third-party participants who contribute to IBKR Campus are independent of Interactive Brokers and Interactive Brokers does not make any representations or warranties concerning the services offered, their past or future performance, or the accuracy of the information provided by the third party. Past performance is no guarantee of future results.

This material is from QuantInsti and is being posted with its permission. The views expressed in this material are solely those of the author and/or QuantInsti and Interactive Brokers is not endorsing or recommending any investment or trading discussed in the material. This material is not and should not be construed as an offer to buy or sell any security. It should not be construed as research or investment advice or a recommendation to buy, sell or hold any security or commodity. This material does not and is not intended to take into account the particular financial conditions, investment objectives or requirements of individual customers. Before acting on this material, you should consider whether it is suitable for your particular circumstances and, as necessary, seek professional advice.

IBKR Campus Newsletters

This website uses cookies to collect usage information in order to offer a better browsing experience. By browsing this site or by clicking on the "ACCEPT COOKIES" button you accept our Cookie Policy.