Object-Oriented Programming in Python

Anmol Sharma
6 min readApr 11, 2021

--

Introduction

Welcome back once again to this series of Blog posts which explores the exciting and innovative world of the Python Programming Language.

Preface

Object-oriented programming is one of the most elementary, foundational and important concepts in the whole of Computer Science. Those who are just starting out with Programming or Even those who are only briefly acquainted Programming may have heard of many Programming Languages such as Python or even Java, Ruby or C# described as Object- Oriented Languages. This means when using these languages, we can apply the Object- Oriented paradigm to expand upon the procedural (simple step by step) code to modular and more compartmentalized programs which increases the complexity and functionality of our programs. Thus, the focus of the post will be on creating and utilizing objects with python, primarily showing how you can transition from Java OOP to Python. As well providing two sample problems.

Warning, before proceeding

Although this post does deal with Object- Oriented Programming and some of its related concepts. This is not a post teaching how OO — Programming works theoretically. But showing you to convert you already honed knowledge of OOP from Java to Python. Such as how a class is written in Java vs Python. And not explain concepts such the methodology of how OOP is applied to problems, a class vs an Object and what a constructor is etc.

Expectations

This is only a rudimentary guide into the world OOP in Python, it only covers the creation of class, initialization of objects etc. It does not cover the other more complex pillars of OOP such as Inheritance and Encapsulation. If I tired to cover all those pillars I, could not pay them full justice here. Trying to cover the complexities of encapsulation would not be of practical value for someone trying to transition from Java OOP to Python OOP and just starting out with Python.

Declaring a Class

As we can see both of these class declarations create the exact same class. We placed within them the same methods and attributes, therefore their functionality and use across both Java and Python will consistent and uniform. The only real the difference in declaring a class lines in first line in both programs. In Java you must specify the access modifier not in Python, and also in Python after the class name you must terminate it with a : before adding any code to the class.

Object initialization

Another minor distinction between Python and Java OOP lies in how both initialization Objects. In Java the keyword new followed by the name of the class is used in order crate a new instance of an Object. In Python this process is much simpler, requires just a variable and then the class name.

Getters and Setters

As you may already know, it is a standard operating procedure to create fields in Java with the private access modifier then implement getter setter methods inside the class. However, this convoluted and complex approach can be totally scrapped, there is no need to create those in Python. You can create them if you so wish, but it totally unnecessary. In python you just access the variable’s directly via the {variable}. attribute.

Constructors

Constructors are a vital part of OO-Programming, there operation in both languages conceptually and in practice is the same. There difference lies in a very simple syntax, change. In Java constructors bare the name of the class to which they belong, in Python the equivalent of constructors is called _init_ and they must contain the keyword self in its parameter list. A noteworthy observation which should be made is that when programming w/ Python you may come across many classes in Python which only feature the _init_ method. This because, as explained earlier Python classes don’t require getters and setters.

Self

One of the most potent keywords which newcomers will cross paths with when doing OO-Programming in Python is self. Since this is syntaxial guide from Java to Python self is comparable to the Java keyword this. Self is Python The self-keyword is used to represent an instance of the class. And just like in Java is Primarily used to fill in the values of attribute of that particular instance.

Practical Example

We’ve now gone over the basics of the creating, initializing and working with objects. We can now move into creating a practical example. Lets imagine we are creating the following scenario: we want to create a OOP solution which allows us to track and store info about customers of an online store.

The solution we decide is to create a Customer class which will hold these attributes:

· Name

· Address

· Id

· Membership Type

· Month Orders

· Month Amount Spent

Feature

· Change membership Type

· Add Monthly Order

· Generate Id

We will work from a pre-built in Java then build it in Python. You can follow along in the demonstration and build it as I am explaining it or you may try it yourself.

Link: https://youtu.be/q1Q75be9rlw

Final Demonstration

As this series of blog posts concludes, its best that end it off by demonstrating a much more advanced demonstration then the one’s we have covered in previous ones. This is a recreation of an Assignment which many SAIT students may have done; The Movie Management System. Here we will be utilizing not only the concepts we just covered about OOP but we will also harp back to thing we covered in the previous posts such as loops if statements, lists and methods. I will first explain the logic of the assignment then compare and contrast it with Java and Python

Link : https://youtu.be/HqA3TFmnHuI

Parting Thoughts

These series of blog posts were intended not as , a full length exhaustive course on the Python programming language but a much more practical guide for those who’s programming background drives from Java to transition to the language in smoother and more clear way. The concepts which were covered herein and in the previous post. Dealt with language basics and OOP basics. You should view this as a foundational piece to a longer journey you should take should the time arrive with the language. Because of its huge diversity of usages in tech and many other industries. It would be wise to try to specialize in one area with this language. But those are just suggestions, thank you and whatever you do I wish you all the very best.

--

--

No responses yet