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.

Object Oriented Programming (OOP) in Python – Part V


Visit: 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

# Commanding the car to 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:

Disclosure: Interactive Brokers

Information posted on IBKR Traders’ Insight that is provided by third-parties and not by Interactive Brokers does NOT constitute a recommendation by Interactive Brokers that you should contract for the services of that third party. Third-party participants who contribute to IBKR Traders’ Insight 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 permission from QuantInsti. The views expressed in this material are solely those of the author and/or QuantInsti and IBKR 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 sell or the solicitation of an offer to buy any security. To the extent that this material discusses general market activity, industry or sector trends or other broad based economic or political conditions, it should not be construed as research or investment advice. To the extent that it includes references to specific securities, commodities, currencies, or other instruments, those references do not constitute a recommendation to buy, sell or hold such security. 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.

In accordance with EU regulation: The statements in this document shall not be considered as an objective or independent explanation of the matters. Please note that this document (a) has not been prepared in accordance with legal requirements designed to promote the independence of investment research, and (b) is not subject to any prohibition on dealing ahead of the dissemination or publication of investment research.

trading top