Tackle the basics of Object-Oriented Programming (OOP) in Python: explore classes, objects, instance methods, attributes and much more!

Object-Oriented programming is a widely used concept to write powerful applications. As a data scientist, you will be required to write applications to process your data, among a range of other things. In this tutorial, you will discover the basics of object-oriented programming in Python. You will learn the following:

  • How to create a class
  • Instantiating objects
  • Adding attributes to a class
  • Defining methods within a class
  • Passing arguments to methods
  • How OOP can be used in Python for finance

OOP: Introduction

Object-oriented programming has some advantages over other design patterns. Development is faster and cheaper, with better software maintainability. This, in turn, leads to higher-quality software, which is also extensible with new methods and attributes. The learning curve is, however, steeper. The concept may be too complex for beginners. Computationally, OOP software is slower, and uses more memory since more lines of code have to be written.

Object-oriented programming is based on the imperative programming paradigm, which uses statements to change a program’s state. It focuses on describing how a program should operate. Examples of imperative programming languages are C, C++, Java, Go, Ruby and Python. This stands in contrast to declarative programming, which focuses on what the computer program should accomplish, without specifying how. Examples are database query languages like SQL and XQuery, where one only tells the computer what data to query from where, but now how to do it.

OOP uses the concept of objects and classes. A class can be thought of as a ‘blueprint’ for objects. These can have their own attributes (characteristics they possess), and methods (actions they perform).

OOP Example

An example of a class is the class Dog. Don’t think of it as a specific dog, or your own dog. We’re describing what a dog is and can do, in general. Dogs usually have a name and age; these are instance attributes. Dogs can also bark; this is a method.

When you talk about a specific dog, you would have an object in programming: an object is an instantiation of a class. This is the basic principle on which object-oriented programming is based. So my dog Ozzy, for example, belongs to the class Dog. His attributes are name = 'Ozzy' and age = '2'. A different dog will have different attributes.

OOP in Python

Python is a great programming language that supports OOP. You will use it to define a class with attributes and methods, which you will then call. Python offers a number of benefits compared to other programming languages like Java, C++ or R. It’s a dynamic language, with high-level data types. This means that development happens much faster than with Java or C++. It does not require the programmer to declare types of variables and arguments. This also makes Python easier to understand and learn for beginners, its code being more readable and intuitive.

How to create a class

To define a class in Python, you can use the class keyword, followed by the class name and a colon. Inside the class, an __init__ method has to be defined with def. This is the initializer that you can later use to instantiate objects. It’s similar to a constructor in Java. __init__ must always be present! It takes one argument: self, which refers to the object itself. Inside the method, the pass keyword is used as of now, because Python expects you to type something there. Remember to use correct indentation!

class Dog:

    def __init__(self):
        pass

Note: self in Python is equivalent to this in C++ or Java.

In this case, you have a (mostly empty) Dog class, but no object yet. Let’s create one!

Instantiating objects

To instantiate an object, type the class name, followed by two brackets. You can assign this to a variable to keep track of the object.

ozzy = Dog()

And print it:

print(ozzy)
<__main__.Dog object at 0x111f47278>

Adding attributes to a class

After printing ozzy, it is clear that this object is a dog. But you haven’t added any attributes yet. Let’s give the Dog class a name and age, by rewriting it:

class Dog:

    def __init__(self, name, age):  
        self.name = name
        self.age = age

You can see that the function now takes two arguments after self: name and age. These then get assigned to self.name and self.age respectively. You can now now create a new ozzy object, with a name and age:

ozzy = Dog("Ozzy", 2)

To access an object’s attributes in Python, you can use the dot notation. This is done by typing the name of the object, followed by a dot and the attribute’s name.

print(ozzy.name)

print(ozzy.age)
Ozzy
2

This can also be combined in a more elaborate sentence:

print(ozzy.name + " is " + str(ozzy.age) + " year(s) old.")
Ozzy is 2 year(s) old.

The str() function is used here to convert the age attribute, which is an integer, to a string, so you can use it in the print() function.

#python #oop #developer

Object-Oriented Programming in Python
2.70 GEEK