Featured post

new redirect for blender.org bpy docs.

http://www.blender.org/api/blender_python_api_current/ As of 10/11 november 2015 we can now link to the current api docs and not be worr...

April 06, 2013

Beautiful, Idiomatic Python

PyconUS 2013 brings many great videos of the event to those of us who didn't manage to go there. transforming-code-into-beautiful-idiomatic-python by Raymond Hettinger is a shining example of the difference between 'functioning' code versus 'fast and beautiful' code. Here's a link to his slides: speakerdeck. Follow Raymond Hettinger's low traffic twitter account @raymondh

If you don't really get what idiomatic means, think of idioms (http://en.wikipedia.org/wiki/Idiom). Idiomatic can refer to expressing an idea in a way that many people can follow - like a figure of speech, or a proverb. Proverbs don't generally translate well between spoken languages, they tend to sound weird, and aren't as 'catchy / funny' or 'to the point' as the same sentiment expressed by a native speaker using the correct proverb for that language.

When speaking about programming languages the concept of the idiom also applies. This is perhaps an unfair example, but it should make a point. If someone uses C++ they might iterate over an array like so
for (int i = 0; i < size_of_array; i += 1) { cout << some_array[i]; }
An exact translation into Python (for the c++ example would be)
some_list = [2,5,34,56,27,45,67]
size_of_list = len(some_list)
for i in range(size_of_list):
    print(some_list[i])

Python offers a tidier 'native' idiomatic way to express iterating over a list
for i in some_list:
    print(i)
C++ has moved on and the latest implementation C++11 offers a for each loop similar to those provided by Python and Java. A for each loop is such a fundamental expressions that the mechanics of setting up the loop and accessing members via indices can done for us by the language. Hence instead of writing them explicitly they can be written in an implied 'idiomatic' way. Over time (or by design) the people most active in the development of a language arrive at preferred way to express common operations.

I recommend watching the video linked to above while pausing and taking notes, here are mine as a note to self for quick reference (as I don't write much Python at present).

4 comments:

  1. Thanks so much for sharing this one! Another Python gems :)

    ReplyDelete
    Replies
    1. Thanks should really go to Raymond Hettinger. what a great talk!

      Delete
    2. Yeah, credit to Raymond as well :)

      By the way, I found that there are strange method such as izip() that does not seem to work in Blender? Did you find similar problem?

      Delete
    3. izip doesn't exist in py3.3, any special features it has are now implemented in the standard zip(). The talk kind of covers py2.7 mostly, without being specific about 3.3. just like py2.7 had range() and xrange() , and those are replaced by range() in py3.

      Delete

Please use Blender.StackExchange.com for python scripting questions unrelated to this post.