Ognen Duzlevski bio photo

Ognen Duzlevski

Senior tinkerer.

Email

I have been programming in Scala for the last few years and let me tell you something - once you go to that functional programming / strong typing thing and once you get used to the compiler checking your work - it is not easy to go back!

The other day I dabbled in some Python (flask) code and I started out wanting to create different configurations to be controlled via a command line switch. In essence, I wanted a dictionary that had two keys - ‘dev’ and ‘production’ and depending on what was passed on the command line, I would have the same set of variables, set differently.

So, for example:

variables = { 
  'dev': {'host':'localhost', port:"8080"},
  'production': {'host':'www.example.org', port:"8888"} 
}

I am sure there are better ways to go about this, but for the sake of argument, let’s accept the above.

But then I changed my mind and I decided to just keep a set of variables. I deleted the curly brackets surrounding everything, deleted the word “variables”, deleted the whole line with the word “production” in it, deleted the comma and deleted the word “dev” and the colon following it.

I was left with:

'host':'localhost',
'port':8080

I ran everything, no syntax error but it did not work. When I tried to concatenate host+port “down the road”, I got a crypric error that elements of a tuple cannot be concatenated. What?

Turns out Python was perfectly happy to “swallow” the above junk code. What I shoud have written, of course, was:

host = 'localhost'
port = 8080

Then host+port would have worked as expected in the concatenation.

The above is, of course, a programmer error through and through. The process by which I arrived at it was a mix of hurry and the fact that I had not touched Python for years. But talk about being able to shoot yourself in the foot in a basic way…