« Arcyria Slime Mould | Main | Teddy Bear Protest »

Explicit is Better than Implicit

I have long admired the programming language Python for its simplicity, unpretentiousness and effectiveness. Earlier this year, I came to appreciate this even more when I was able to use it in a project at work.  On several occasions I ran into what appeared to be insuperable problems only to discover that Guido (van Rossum - the designer of Python) had been there before and had provided a minimalist, practical solution.  It really was a pleasure to use.

However, there have always been a couple of very basic things which I feel that Python has got wrong.  These are not so much problems for experienced programmers, who can easily work round them, as they are for beginners, who almost always trip up over one them.  They seem so unneccesary and it worries me that they are still there, to the extent that I always hesitate before recommending Python as a beginner's language.

The first is using leading zeros to indicate octal numbers.  For example: 101 = 101, 110 = 110, but 011 = 9. This feature appears to have been inherited from C.  Octal is more or less extinct now and, for those applications that need it, it would be far better to have an explicit indicator rather than this underhand subterfuge from the dim and distant past.  Python should at least have a command-line option to allow you to choose whether you want it or not.

The second problem arose yesterday when my daughter was doing her maths homework.  She had some calculations to do involving numbers raised to various powers but couldn't find her calculator so I suggested that she use Python.  She typed in something like (1/4)**2 and got the answer 0, which is not what she was expecting.  The problem is that in Python the symbol '/' represents two different operations according to the types of its operands.  If one or both of x and y are floating point numbers then x/y respresents a floating point number approximating z such that x = y*z.  But if x and y are both integers then x/y returns an integer n such that n <= z < n+1 where x = y*z.  This 'integer' or 'floor' division is a completely different operation from normal division and it should (again) really be represented by a different notation.  Fortunately, another notation already exists in Python: '//'.  However, if you want '/' to represent proper division you have to make it explict that at least one of its operands is a floating point number and the best way to do that is to use the float() constructor: float(x)/float(y).

I wasn't about to explain all that to my daughter (and she wouldn't have listened anyway), so I told her to use the Ubuntu calculator, gcalctool, instead.  This has a scientific mode in which she was able to enter (1/4)^2 and get the right answer, 0.0625.

(The title of this post is taken from Tim Peters' Zen of Python.)

Reader Comments

There are no comments for this journal entry. To create a new comment, use the form below.

PostPost a New Comment

Enter your information below to add a new comment.
Author Email (optional):
Author URL (optional):
All HTML will be escaped. Hyperlinks will be created for URLs automatically.