Python. Good, bad, evil -1-: Missing brace - Blog - Open Source - schlitt.info

schlitt.info - php, photography and private stuff

Python. Good, bad, evil -1-: Missing braces

Every programmer should learn a new programming language once in a while. Be it for inspiration or just for fun. After some homework in C# some years ago and quite some Java at university (again), Python was my language of choice. This is the first article in a series I plan to write about what I like in Python, what is disturbing but works out and what is really evil. In this article I give a short overview on my motivation to dig into Python and start with a first topic: The missing braces.

This entry is part of my series Python. Good, bad, evil, where I discuss my experiences with Python from a PHP developers point of view. Find references to the successor episodes in the trackbacks of this post.

Learning a language just for the sake of doing so is cumbersome. My motivation for it was, that I needed a well established and responsible ID3 tagging library. Neither a PHP lib nor extension, nor any shell tool I found satisfied my needs, but Pythons mutagen did.

After having tagged my MP3 collection quite well using Musicbrainz Picard, I wanted to have reasonable genre tags for this, too. There is a plug-in for Picard, which tries to determine genres from Last.FM tags for a specific title by a specific artist. I liked the idea, but the plug-in has some serious drawbacks. So I finally decided to take the jump into Python and started with Pagger, a semi-automatic solution for genre tagging on the shell.

Beside the very nice mutagen library, Python is one of the most used scripting languages nowadays. As PHP does, Python allows you to code object oriented, procedural and functional. In this sense it is quite similar to my favorite language. However, Python has some fundamental differences in respect to syntax and semantics, which I found interesting to experience. In addition, learning about different approaches from outside ones own community is always a good idea.

In each article in this series, I will tackle another feature I found interesting while working with and reading about Python. I will show you some small examples to illustrate the discussed feature, discuss the pros and cons I see and conclude with an opinion if I would like to see such an approach in PHP or not. I'd love to read your opinion in the comments, so feel free to discuss your own and my views.

Pagger

This should not be an article about Pagger, but once mentioned, I'd like to give a short overview on what it does: Pagger runs on the shell and receives a config file and a directory with MP3s as parameters. The config file determines the genres you want to be used, mappings between tags and genres, ignore patterns for tags and some more configuration.

Pagger now tries to determine suitable genres for an MP3 from Last.FM tags and from Freebase. If it recognizes all of these genres, it automatically assigns them without interaction. If there are unrecognized genres for a file or none at all, it offers an interactive shell to add new genres, new mappings, ignores regex and more. Learning iteratively this way, Pagger has the goal to assign tags more and more sensible automatically with each tagged file.

You can find the Pagger source code on github. Feedback and contributions are very welcome. But beware, this stuff is below alpha state by now.

The no-brace approach

In contrast to many other languages, Python does not use braces of any kind for syntactical code structure. Don't be too shocked, when comparing the following two code pieces, that perform equivalent operations in PHP and Python:

<?php for ($i = 1; $i <= 10; $i++) { if ($i % 2 === 0) { echo "$i is even\n"; } } ?>

In Python one could achieve the same thing like this:

for i in range(1, 11): if i % 2 == 0: print i, u" is even"

I know that there are more elegant ways to achieve this in both languages, but examples should be simple and stupid.

Cons

I'll start with the cons of such an approach: First, Python code looks simply ugly for programmers that are used to common languages like PHP, Java, C and many others. Braces are the most common approach for structuring source code at all. Therefore, many many coders out there are highly used to that way and are pretty irritated by Python code missing them. It takes quite a while to get used to it, which keeps you from efficient coding at first.

Second, problems occur easily here, when inconsistent indentation is used. Python only looks for the indentation in number of space characters. This might be blanks, but also tabs (and maybe other space chars?). You need to stick carefully to your style of indentation, otherwise your code will break, not only the optical appearance. However, this can also be considered a pro in terms of coding style, as you will see in the next section. And a sensible editor will fix the issues for you automatically.

Braces produce many blank lines in your code. While this might sound senseless at a first glance, this allows you to visually detect mating structures more easily. Python code looks generally more compact, if you do not introduce blank lines by intention. Having to use braces forces a developer to do so, which helps the readability of code.

Pros

No matter in which language you program, sticking to a common coding style throughout a project or maybe even a whole community provides only advantages. Digging into code which looks like it could be your own (from an optical perspective) makes you feel comfortable and therefore eases the process of analyzing the code. Enforcing a common coding style is therefore a goal in many programming communities. Python simply ensures that every code piece at least appears highly similar to every Python programmer.

Typing braces over and over again is cumbersome. Although most developers won't even notice any more, every brace costs a little bit of time. In addition, some keyboard layouts make typing braces hard: For example on a German keyboard, you need to type <Alt-Gr> + <7> to achieve a {. Removing the need to type braces therefore reduces time and non-productive effort while coding. Although both affect you only a very little.

Conclusion

Code without braces looks and feels strange to many programmers, but having the compiler enforce a certain coding style is cool. However, beside the fact that it won't happen, I also would not want PHP to loose its braces. What I would like to see is a common coding standard which can be enforced by PHP using an INI setting.

Naturally, this would be the coding standard I am using, just for clarity. ;)

I will continue this series whenever I find some time to blog and no hot topic is in the pipe. This won't happen before March 1st, since I'm on vacation for a week now. Read you in a bit! :)

If you liked this blog post or learned something, please consider using flattr to contribute back: .

Trackbacks

Comments

Add new comment

Fields with bold names are mandatory.