Ex­plor­ing Soft­ware: Python is Still Spe­cial

The au­thor takes a good look at Python and dis­cov­ers that he is as par­tial to it af­ter years of us­ing it, as when he first dis­cov­ered it. He shares his rea­sons with read­ers.

OpenSource For You - - Contents - Anil Seth

It was in the year 2000 that I had first come across Python in the Linux Jour­nal, a magazine that’s no longer published. I read about it in a re­view ti­tled ‘Why Python’ by Eric Ray­mond. I had loved the idea of a lan­guage that en­forced in­den­ta­tion for ob­vi­ous rea­sons. It was a pain to keep re­quest­ing col­leagues to in­dent the code. IDEs were prim­i­tive then—not even as good as a simple text ed­i­tor to­day.

How­ever, one of Ray­mond’s state­ments that stayed in my mind was, “I was gen­er­at­ing work­ing code nearly as fast as I could type.”

It is hard to ex­plain but some­how the syn­tax of Python of­fers min­i­mal re­sis­tance!

The sig­nif­i­cance of Python even to­day is un­der­lined by the fact that Uber has just open sourced its AI tool Pyro, which aims at ‘…deep uni­ver­sal prob­a­bilis­tic pro­gram­ming with Python and PyTorch (https://eng. uber.com/pyro/).’

Mozilla’s Deep­Speech open source speech recog­ni­tion model in­cludes pre-built pack­ages for Python (https://goo.gl/nxXz2Y).

Pass­ing a func­tion as a pa­ram­e­ter

Years ago, af­ter cod­ing a num­ber of forms, it was ob­vi­ous that han­dling user in­ter­face forms re­quired the same logic, ex­cept for val­i­da­tions. You could code a common val­i­da­tions rou­tine, which used a form iden­ti­fier to ex­e­cute the re­quired code. How­ever, as the num­ber of forms in­creased, it was ob­vi­ously a messy so­lu­tion. The abil­ity to pass a func­tion as a pa­ram­e­ter in Pas­cal, sim­pli­fied the code a lot.

So, the fact that Python can do it as well is noth­ing spe­cial. How­ever, ex­am­ine the simple ex­am­ple that fol­lows. There should be no dif­fi­culty in read­ing the code and un­der­stand­ing its in­tent.

>>> def add(x,y): … re­turn x+y ...

>>> def prod(x,y): … re­turn x*y


>>> def op(fn,x,y): … re­turn fn(x,y) …

>>> op(add,4,5)


>>> op(prod,4,5)



All too of­ten, the method re­quired is determined by the data. For ex­am­ple, a form-ID is used to call an ap­pro­pri­ate val­i­da­tion method. This, in turn, re­sults in a set of con­di­tional state­ments which ob­scure the code.

Con­sider the fol­low­ing il­lus­tra­tion:

>>> def op2(fname,x,y): … fn = eval(fname) … re­turn fn(x,y)


>>> op2(‘add’,4,5)


>>> op2(‘prod’,4,5)



The eval func­tion al­lows you to con­vert a string into code. This elim­i­nates the need for the con­di­tional ex­pres­sions dis­cussed above.

Now, con­sider the fol­low­ing addition:

>>> newfn =”””def div(x,y): … re­turn x/y”””

>>> exec(newfn)

>>> div(6,2)


>>> op(div,6,2)


>>> op2(‘div’,6,2)

3 >>>

In the ex­am­ple above, func­tion has been added to the ap­pli­ca­tion at run­time. Again, the em­pha­sis is not on the fact that this can be done, but con­sider the sim­plic­ity and read­abil­ity of the code. A per­son does not have to even know Python to get the idea of what the code in­tends to do.

Prof. Di­jk­stra wrote, “If you want more ef­fec­tive pro­gram­mers, you will dis­cover that they should not waste their time de­bug­ging; they should not in­tro­duce the bugs to start with.” (https://en.wikipedia.org/wiki/ Eds­ger_W._Di­jk­stra)

This is where Python ap­pears to do well. It ap­pears to al­low you to pro­gram fairly com­plex al­go­rithms con­cisely and re­tain read­abil­ity. Hence, the like­li­hood of in­tro­duc­ing bugs to start with is min­imised.

On the im­por­tance of which pro­gram­ming lan­guages to teach, Prof. Di­jk­stra wrote, “It is not only the vi­olin that shapes the vi­o­lin­ist; we are all shaped by the tools we train our­selves to use, and in this re­spect, pro­gram­ming lan­guages have a de­vi­ous in­flu­ence: they shape our think­ing habits.” (http://chris­done.com/posts/di­jk­stra-haskell-java)

It is not sur­pris­ing that Python is widely used for AI. It is easy to in­te­grate Python with C/C++, and it has a wide range of in­built li­braries. But most of all, it is easy to ex­per­i­ment with new ideas and ex­plore pro­to­types in Python.

It is def­i­nitely a lan­guage all pro­gram­mers should in­clude in their tool­kits.

Newspapers in English

Newspapers from India

© PressReader. All rights reserved.