Paradigms of python programming- programming fads

Ever thought that you can associate a term like “style” to a programming language, well actually, you can associate so many of these fashion terms. But first let me tell you more about the python language, before I interest you on this idea. Admittedly, Python is my favorite language considering my limited knowledge and understanding of the subject I find python easy and potent enough to solve my problems. The language has grown with its history, it has come far, to become the most sought after language of the present decade. From data science (which is the most powerful asset of the language) to web development to building applications this language is now widely used. Python striking features are the environment and the ease of syntax. I may not be able to convince anyone as I have not been able to convince my economics genius sister (who is still grappling with modern day programming languages) about a certain charm this language carries, well one of the IDE’s of python is pycharm. Fallen for it already.

We started with “style” and “fashion terms” and how can you associate these terms to python programming language. In Programming you can associate “style” to what style a programmer chooses to write his code similar to what style of clothes you choose to wear, different style of clothes for different purpose. And terms like “hand-me-down” if you know git and github. we will come back to it soon. Let us move our focus back to paradigms of programming language.

Python is a multi-paradigm language, which means it supports various coding designs (it refers to the way programmer approaches a coding problem in terms of writing the code). It is like “freedom of expression” (well absolutely not like when you critique the government it is considered anti-national). It is more like where a coder/Programmer is given the freedom to choose his style to code the solution of a problem.

Consider python as a medley of programming paradigm

Since Python is a multi-paradigm programming language, you can choose the paradigm that best suits the problem at hand, mix different paradigms in one program, and/or switch from one paradigm to another as your program evolves.

Programming is communicating with computer mainly for performing a specific task. This communication with computer sometimes becomes complicated. A paradigm is the model or style or a standard that a programmer follows for an effective communication. The term language paradigm is sometimes used to describe the mental model that the programmer envisions as he or she is creating a program.

That is his style

What is Imperative Paradigm lets revisit again.

1. This model views the computer as a combination of processor and memory. Instructions (such as assignment statements) have the effect of making changes to memory. The task of programming consists of placing statements in their proper sequence, so that by a large number of small transformations to memory the desired result is eventually produced.

2. Computation is performed as a direct change to program state. This style is especially useful when manipulating data structures and produces elegant yet simple code. Python fully implements this paradigm. In imperative programming, you focus on how a program operates. Programs change state information as needed in order to achieve a goal.

Let us see an example of imperative programming

Task : Take the sum of the list

my_list = [1, 2, 3, 4, 5]

The value of sum changes with each iteration of the loop. As a result, sum has state. When a variable has state, something must maintain that state, which means that the variable is tied to a specific processor. Imperative coding works on simple applications, but code executes too slowly for optimal results on complex data science applications.

The traditional imperative style of programming produces complex values by modification; by making a large number of small changes to an existing data structure. For example, creating a dictionary of values, then systematically setting each value independently of the others. Since small changes can often be accompanied by small errors, and small errors may only produce a minimal effect, debugging imperative programs can be frustratingly difficult.

Functional Style

From the name you would be getting the idea that it must involve programming by building or using lots of functions, yes it is but it is not only just functions. In fact the key characteristic of a program developed in the functional programming style is that it creates new values by a process of transformation.

By emphasizing transformation, rather than modification, functional programs work on a larger scale. Transformations are often more uniform, and much simpler to write and debug. Errors, when they do occur, tend to be larger and thus easier to find and eliminate.

  • The process of transformation can be subdivided into several common forms. The three most common varieties of transformation are mapping, filtering, and reduction.

We have seen these three techniques but just a quick summary of each of these with examples.

Mapping :

is a one to one transformation. Each element in the source is converted into a new value. The new values are gathered into a collection, leaving the original collection unchanged. For example, suppose that you begin with the list [1, 2, 3, 4, 5] and map using the transformation x*2+1. The result would be the list [3, 5, 7, 9, 11].

Example:

Filtering :

is the process of testing each value in a list with a function, and retaining only those for which the function is true. If you begin with the list [1, 2, 3, 4, 5] and filter with a function that returns true on the odd values, the result would be the list [1, 3, 5].


Reduction :

is the process of applying a binary function to each member of a list in a cumulative fashion. If you begin with the list [1, 2, 3, 4, 5] and reduce using the addition operation, the result would be ((((1 + 2) + 3) + 4) + 5), or 15.

Note on reduce:

  • Applies same operation to items of sequence.
  • Uses result of an operation as first parameter of next operation.
  • Returns an item not a list

All of these functions are using functions as arguments. A function that uses another function that is passed as an argument is sometimes referred to as a higher-order.

let us compare the two style

Suppose you have two lists (call them a and b), and you need to construct a third list containing their intersection. That is, the third list should have only those elements that are found in both lists.

Let us see this with functional programming

There is also a procedural Programming which we have seen in C if you are from an engineering background.

What is Procedural Programming?

This is one of the oldest form of programming. It is similar to imperative style you can call it as a sub-type of imperative programming. Here each procedure is a sequence of statements that has a specific defined purpose.It is based on the concept of the procedure call. The procedures are sometimes also referred to as routine subroutines and functions. In procedural programming the code of the program executes linearly with logical steps, it follows the top to bottom approach. So basically, you combine these procedures to instruct a computer at each step it has to take to perform a certain task. Procedures are usually modularized by a function definition which provides a very simple interface to be used by others. It flows the program in a linear direction which makes it easy to track. It simplifies the code and makes it easy to understand.

Now It is the time to see the Object oriented programming.

First what is objected oriented programming and why object oriented programming?

In the object-oriented paradigm, a program is viewed as a collection of computing agents, each of which is providing a service that can be used by the others.

Well before we begin to elaborate this paradigm or style, we will move into history just a tad bit to understand what led to the development of this style, was that the other style became too boring or was this just an addition to the already available styles. The idea here is to see this type of paradigm exlusively. Later, it shall pave the way to make the other paradigms easier to understand and differentiate between them clearly.

From my reserach on understanding this topic, there are two articles that did a good job, one by steve jobs and other by Alan Kay

Alan Kay:

The central idea is that object-oriented programming is a way of structuring software systems analogous to a biological system, with “cells” (which we’ll call “objects”) that hide their internals, and communicate with other objects via a kind of signalling mechanism (which we’ll call “messaging”).

Before we go and discuss stevejobs, Every other paradigm we discussed above has its merits and demerits, Though one is not limited to use either of the paradigm and you can even combine them. It is certainly difficult to weigh one over the other and also compare them. It is not right to say that development was the reason. But one thing that stick out to me is managing a huge code or writing complex code. Procedural and Oop both do a wonderful job. Procedural does this with functions or procedure which can even be reused. But Oop does make a tad bit better with class, objects and its properties like inheritence, polymorphism, etc. This is just my opinion. For me it would be difficult to make somebody undestand this until someone attempts on their own. Let us Proceed and futher i will explain the properties of OOP.

The idea is to choose a method which helps us in organizing our thoughts better, and help us in building programs, that are much easier to understand and serve our purpose.

Steve Jobs Example :

Jeff Goodell:

Would you explain, in simple terms, exactly what object-oriented software is?

Steve Jobs:

Objects are like people. They’re living, breathing things that have knowledge inside them about how to do things and have memory inside them so they can remember things. And rather than interacting with them at a very low level, you interact with them at a very high level of abstraction, like we’re doing right here.

Here’s an example:

If I’m your laundry object, you can give me your dirty clothes and send me a message that says, “Can you get my clothes laundered, please.” I happen to know where the best laundry place in San Francisco is. And I speak English, and I have dollars in my pockets. So I go out and hail a taxicab and tell the driver to take me to this place in San Francisco. I go get your clothes laundered, I jump back in the cab, I get back here. I give you your clean clothes and say, “Here are your clean clothes.”

You have no idea how I did that. You have no knowledge of the laundry place. Maybe you speak French, and you can’t even hail a taxi. You can’t pay for one, you don’t have dollars in your pocket. Yet I knew how to do all of that. And you didn’t have to know any of it. All that complexity was hidden inside of me, and we were able to interact at a very high level of abstraction. That’s what objects are. They encapsulate complexity, and the interfaces to that complexity are high level.

I hope the example would have given you an intuitive understanding of object oriented programming. The Crux of this are objects.

Objects:

“object” refers to a particular instance of a class where the object can be a combination of variables, functions, and data structures.

An object is an entity that possesses both state (or properties or attributes) and behaviour. Put another way, an object encapsulates data and the functions that operate on that data. The data is usually hidden from other objects so that the only way to affect the data is through the object’s functions (or methods).

an object is something which has some property and does some tasks which are assigned to it.

All the objects are enclosed in a wrapper called a class which represents the object as a whole

objects vs class

Class

1.It is like a template or a blueprint a specific one.

2.A class is used to bind data as well as methods together as a single unit.

3.The class has to be declared only once.

Object

1.An object is the instance of the class, which helps programmers to use variables and methods from inside the class.

2.Object acts like a variable of the class.

3.Objects can be declared several times depending on the requirement.

more to follow in the next post.

One thought on “Paradigms of python programming- programming fads

Leave a comment