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 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.