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 IV


Visit: QuantInsti

Get started with Part I,  Part II and Part III.

What are classes and their objects?

Let’s continue with the example of a car. If we think in abstract terms, a car is nothing but the replication of an abstract idea. That is, a car itself is a generic term used to define a particular type of vehicle. In other words, it is one of the classes of vehicles. In programming terminology, we represent this abstract idea by a class.

Now, let’s think for a minute. If we say that a car is a concept, then what do we call a particular car, such as Toyota Corolla (or any of your favourite ones), what is it? As you might have guessed, it is an object of the car. And what is a car? It is a class (probably under the vehicle universe).

If we take an abstract look, we find that these cars are nothing but the replication of one abstract thing (idea) with different attributes and functions. In programming parlance, this thing is class. In other words, the concept of a car provides us with a template or a blueprint from which we can define/create various objects of the car.

Can you try to think of some other classes and their objects?

Below are some examples:




iPhone X




Mr Bean


R 18

At this juncture, I firmly assume that I was able to convey the idea of classes and objects to you. If not, do let me know in a comment on our blog (via the link below).

It’s time to learn to implement these newly learned concepts in Python. The below code shows a class definition in Python.

class Car:

We define a class with the help of a keyword class, followed by the name of the class, and we end the sentence with :. The body of the class should contain its attributes and functions.

However, we define the class Car with an empty body represented by the keyword pass. In the OOP paradigm, functions that go within the class are referred to as methods; now onwards, I will refer to functions as methods.

Once we have a class defined, we can create its instances, known as objects. The class Car works as a template to create new objects, as shown in the example below:

car_1 = Car()
car_2 = Car()

Often when we create an object of a class, we assign it to some variable. That variable is called the object of the class. Here, in our example, car_1 and car_2 are examples of the class Car. This process is also known as the instantiating of an object. These objects are also known as class instances.

Each of these objects can have different properties, and they can be set as follows:

car_1.colour = ‘Carbon Black’
car_2.colour = ‘Magma Grey’

Now, both objects have the colour attribute. And if we are to print it, we would write as follows:

print(‘The colour of Car 1 is ‘, car_1.colour)
print(‘The colour of Car 2 is ‘, car_2.colour)

And the output we will get is the following:

The colour of Car 1 is Carbon Black
The colour of Car 2 is Magma Grey

So far, we have created a class Car and its objects car_1 and car_2. However, currently, the Car class in its current form can hardly be mapped to its real-world counterpart. For example, we know that every car will have certain features in common, like colour, number of types, number of seats, etc., and so some functions. Hence, instead of defining an empty class, we can define a class which encompasses these common attributes and functions.

Stay tuned for the next installment in which Jay will discuss attributes and methods.

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.

trading top