Get started with Object Oriented Programming in Python: Classes and Instances

Learn how to write a class and create instances in PythonEmilee SmithBlockedUnblockFollowFollowingMay 14There are a lot of articles popping up on object-oriented programming in Python at the moment.

Many data scientists, myself included, find ourselves in roles that focus on writing functional code, often in small scripts or prototypes.

I’ve been working as a data scientist for 3 years (and previously spent a few years as a data analyst) and up until now I’ve had little practical exposure to object-orientated programming.

This seems to be fairly common in the data science world — many data scientists work for years in the industry without ever needing to write code based on object-oriented programming concepts.

There are also a fair number of arguments against implementing OOP principles.

These generally include writing code that has high maintenance overheads, which becomes increasingly complex to work with.

But as the industry matures, there is a need for data scientists to write production ready code and to integrate better with engineering teams.

Having some basic understanding of OOP and being able to write classes and methods can be very helpful.

Additionally, if you ever wanted to contribute to an open source Data Science project the chances are you would be dealing with objects, and would need some knowledge of OOP!Step 1: Write your first class in PythonThe basic idea behind OOP is to have a number of objects that manage single simple tasks, all of which make up a complex computer program.

The starting point for OOP is to understand what a class is.

A widely used definition for class is that classes are “blueprints for creating objects”.

A class is a logical grouping of data and methods (methods are similar to functions).

Classes are often based on objects we find in our business world: customers, products, employees.

As a financial services business we store and work with customer information.

We can create a simple class called customer:In OOP we have the class keyword, data scientists will be familiar with the functional equivalent def.

def is used to define a function (or method when its inside a class) and similarly, class is used to define a class.

Defining the customer class doesn’t actually create any customers, we’ve simply made a blueprint for creating customer objects.

The first thing you will notice inside our customer class is the __init__ method.

The __init__ method is a special method in Python classes, it is called whenever a class is constructed.

__init__ is the constructor for a classEvery time you create a new class you will call the __init__ method and you will also use the self argument.

The self variable represents the instance of the object itself.

Some OO languages will pass this as a hidden parameter to the methods defined but in Python we need to declare it explicitly.

the self parameter explicitly represents the instance of the objectAfter we have defined the __init__ method and specified the self parameter we list the other parameters / arguments that will be used in the __init__ method.

In this example these are first, last, mobile and monthly.

We have now created our class customer and would go on to create some instances of that class.

Step 2: Instances and methodsAn instance, in object-oriented programming (OOP), is a specific representation of any object.

An object is a generic thing while an instance is a single object that has been created in memory.

Usually an instance will have values assigned to it’s properties that differentiates it from other instances of the type of object.

Creating an instanceThe code below creates an instance called cust1.

You would do this every time you need to store new customer information.

cust1 = Customer('Emilee', 'Smith', '0821231234', 20000)cust1 is an instancecustomer is the classCreating a methodMethods are similar to functions, except they are found inside your class.

In this example we are going to write a simple method called annual inside our customer class.

This will simply take the monthly salary listed for the customer and calculate the annual salary.

Each method within a class automatically takes the instance as the first argument, which means you pass the self argument in parenthesis.

If you leave out the self argument then you will get an error later when you call it.

To have a look at what is happening in our annual method we can simply print it:print(cust1.

annual())>> Emilee Smith 240000Useful note: In Python if you wish to call an attribute, for example “fullname” in the customer class, then you can simply do it like this:print(cust1.

fullname)>> Emilee SmithWhen you call a method (rather than an attribute) you need to use parenthesis like we did when we called annual.

You have now created a new class called customer, an instance called cust1 and a method called annual.

These are the first practical building blocks of writing OOP code in python!If you’re planning to take your OOP programming further you should read up on the four principals of OOP: encapsulation, abstraction, inheritance, and polymorphism.

Here’s a useful resource for this:How to explain object-oriented programming concepts to a 6-year-oldHave you noticed how the same cliche questions always get asked at job interviews — over and over again?medium.

freecodecamp.

org.. More details

Leave a Reply