Python Labs : Different Programming Styles

Madhawa Perera
5 min readMay 29, 2019

--

Python is an object-oriented programming(OOP) language. However, in contrast to many other OOP programming languages, Python is simple and incredibly flexible. Hence, the developers can choose a programming style which best suited for them to solve a particular problem efficiently. When I say different styles or programming styles, it means the ways that you can write your code(Hereafter I will refer to this as style). Some use the term programming paradigm. Mainly Python allows you to write programs in 3 different styles. Those are: object-oriented, procedural and imperative and functional. Do not worry what it means right now. I will take a single example and show how to write the solution in all 3 different styles. Before jump into coding let's look at the what these terms really mean.

1. Object-oriented

This is style will create reusable patterns/templates of code. This has its own set of concepts. Classes, Objects, Inheritance, Encapsulation, Polymorphism, Abstraction etc. Explaining the whole idea with a few sentences is a bit difficult task (It depend on your level of knowledge). Yet, if you like to further dig into this style, there are plenty of online resources. You can find a nice beginner guide here in DataCamp. Some people argue that Python does not support this paradigm fully as it could not stick with concepts like encapsulation. Yet, this style is useable when you have to deal with templates, structures and code reuse.

2. Procedural and Imperative

Some define procedural programming as a subtype of imperative programming. Unlike the object-oriented style, this uses a linear or top-down approach. In procedural programming, developers use step-by-step iterations where common tasks are placed in functions that are called whenever necessary. Well, here also there's a little bit of reuse of the code but it isn't the prominent aspect here. Python excels in implementing this particular paradigm.

Imperative programming style is used with both object-oriented and procedural programming styles yet the important fact here is, with Python, you can use this style independently without the help of the above two styles. There the focus is on creating statements that change program states by creating algorithms that tell the computer how to do things. This style is especially useful when manipulating data structures. Again Python fully supports this style.

3. Functional

Python isn't purely supporting this style because Python is not a functional programming language but it does incorporate some of its concepts (If you like functional programming style, then Haskell may be a better choice). "Functional languages are declarative languages, they tell the computer what result they want. This is usually contrasted with imperative languages that tell the computer what steps to take to solve a problem". Learn Functional Python in 10 Minutes is a good read if you are interested in learning more about this.

All the above are like theory and some high-level definitions for you to have a bigger picture of what all these terms mean. Let's take a simple example.

We have a list of number and we want to calculate the sum of those number in the list. Let's do it using all four types of programming styles.

sample_list1 = [10,20,30,40] and sample_list2 = [50,60,70,80]

Let’s calculate the sum of numbers in example_list using object-oriented programming style.

Sample_Code_1

In the Sample_Code_1, we use the ListManipulator template to create its instances. The terminology used for these types of templates in OOP is 'class'. Class is like a stencil/template. You can create any number of objects/instances from ListManipulator stencil and use its methods. You don't need to write the whole set of lines again. Instead, you call the method on the created object.

Notice that listManipulator1 object is created with the sample_list1. When I calllistManipulator1.any_list, it gives me the sample_list1 because we used sample_list1 to create listManipulator1 object. Similarly, listManipulator2 is created with sample_list2. When we call the same functions on listManipulator2 instances, it returns the total of the sample_list2. Thus, you can see that ListManipulator is the template and listManipulator1 and listManipulator2 are the instances/objects created using that template. Therefore we have reused the ListManipulator template code.

Inside the ListManipulator class, I have created 2 methods which are doing the same operation. This was done purposely to show, even though I used object-oriented programming style within the code I have used imperative style as well. So it's always better to have an understanding of all these types.

Let’s calculate the sum of numbers in example_list using the procedural and imperative programming style.

Sample_Code_2

In Sample_Code_2 you can see that I have reused the function calculate_total() to calculate the sample_list1 and sample_list2 total. The procedural style relies on procedure calls to create modularized code. Which means you can use functions to group the similar type of work and simplifies the overall code.

Sample_Code_3

Sample_Code_3 is an example of the imperative style. From the first glance you will see that there isn't much difference between procedural and Imperative styles. The reason is procedural style uses imperative programming. Yet, the focus is on how the program operates. Programs change state information as needed in order to achieve a goal are called imperative. In here the value of the variable x and total changes as instructed by the developer. So we say that these variables have states. When a variable has states, that the variable is tied with a specific processor which helps this state transition. We use this style mostly when working with data structures. However, within the procedural and object-oriented programming styles, you will see this imperative style more often. But Functional program holds a different approach.

Let’s calculate the sum of numbers in example_list using the functional programming style.

Sample_Code_4

In functional programming, we don’t tell the computer what to do but rather you tell it what stuff is. In the functional programming, changing variables (in the pure functional program they don't use the term variables) something which we don't see. Also, you won't see functions affect things outside of their scope. Like changing global variables values. In the functional programming paradigm, if a function is called twice with the same parameters, it’s guaranteed to return the same result. There will be no side effects such as changing another variable's value which affect another function etc. This is very similar to mathematical functions. Also, in functional programming, we do not use loops, instead, you will see recursion. I know the example that I have taken actually reflect the differences a lot but I think it makes sense. If you have a better example please feel to comment below. As always please feel free to give your feedback and constructive criticisms. Keep learning!

If you like this article and the content that I create, you can buy me a virtual coffee ️😊;

Cheers!

References

  1. https://stackabuse.com/functional-programming-in-python/
  2. https://hackernoon.com/learn-functional-python-in-10-minutes-to-2d1651dece6f
  3. https://blog.newrelic.com/engineering/python-programming-styles/
  4. https://www.datacamp.com/community/tutorials/python-oop-tutorial

--

--

Madhawa Perera
Madhawa Perera

Written by Madhawa Perera

Research Engineer, HCI Researcher, and Sessional Academic at ANU. Passionate about crafting human-centered AI experiences through Mixed Reality (AR/VR)

No responses yet