Object Oriented Programming (OOP) in Python – Part VI


Visit: QuantInsti

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

What is init method?

You might have guessed what __init__ does and means? If not, here you go, __init__ means initialisation. We use this method to initiate the attributes with values provided by the object when it gets created. In other words, the __init__ methods gets called as soon as a new object is created. Let’s implement it in our Car class and see how we can leverage it.

class Car:

def __init__(self, uColour, uSeatingCapacity):

self.colour = uColour
self.seating_capacity = uSeatingCapacity

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

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

In this implementation of the class, we define all variables (and methods) in the __init__ that needs to be assigned (and called) upon creating a new object. How? As demonstrated in the below code:

car_4 = Car(uColour=’Ocean Blue’, uSeatingCapacity=2)

We provide the values to be assigned to colour and seating_capacity attributes while creating an object. This way, we can overcome the requirement to set each object’s attribute values after they have been created.

If we access the newly created object’s attributes, it would have the values we provided while creating them.

print(‘The colour of Car 4 is:’, car_4.colour)
print(‘The seating capacity of Car 4 is:’, car_4.seating_capacity)

The output would be as shown below:

The colour of Car 4 is: Ocean Blue
The seating capacity of Car 4 is: 2

We can also place a method within the body of __init__ to ensure that the method gets executed upon creation of an object. Those of you who come from any other object-oriented programming language should be able to relate the __init__ method with the constructor method.

You might have noticed that while defining these methods, __init__ or any normal for that purpose, the first parameter these methods take is the self keyword.

Why is this keyword necessary, and why do we need to provide this keyword? We discuss it next.

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

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.

Any trading symbols displayed are for illustrative purposes only and are not intended to portray recommendations.