Tuesday 2 November 2010 — This is over 12 years old. Be careful.
A nice question on Stack Overflow today drew good answers, but one of them chided,
Stop saying “pythonic” when you mean “clean”. It’s just a cheesy buzzword.
It made me think about what people mean by “pythonic,” I think it is more than simply “clean.” I think it also involves appropriate use of the language and standard library features. It of course includes “clean” but I think you can write clean Java-like code in Python, and it will not be pythonic. The pythonic label has to do with a minimalism, getting more done than seems possible with a small amount of code.
But I also wonder, why does Python have an adjective, when other languages do not? We don’t hear about code being Javanese, or Pearly, or Rubinesque. Why don’t we speak of code that is C-plush-plush, or PHPleasing? Why does Python have an adjective?
PS: any other interesting proposals for language adjectives?
It's impossible for certain languages on your list to have a design aesthetic. PHP and Java are too clunky. Also consider that Perl (and to some extent Ruby) would *value* the lack of a strong aesthetic. The presence of many ways to do things coupled with the lack of a clear best way is a postmodern virtue.
@Brett please for the love of... no WWGD stickers.....
It's also interesting that what is Pythonic has changed as Python has matured---Python 3 is the latest iteration in this process as the old idioms have been dropped for what is the modern core of the language.
I would have liked to have seen even more of the standard library cleaned up as part of Python 3. A lot of the modules from the 1.x days don't feel very pythonic to me. Still, Python 3 shipped and it might still be in development if they tried to do all that too.
That's how I'd define the Pythonic way of writing code. You know it when you see it.
Python offers however a wide range of improvements over those basic commands: list comprehensions, named arguments, default arguments, "for elt in some_list", dictionaries, generators, and so on.
To me, being pythonic is to use that range of Python improvement vs programming in a dull way as in any other language.
"for i in range(len(L)): print L[i]" -> not pythonic. "for elt inL: print elt" -> pythonic.
"L[len(L)-1]"-> not pythonic. "L[-1]"->pythonic.
@brett: as much as I would like to say that Guido doesn't always win "is it pythonic" debates, I have a feeling he probably always does!
@nickf: "ajaxy" reminds me of "enterprisey", there are probably more language-agnostic adjectives of their ilk.
@masukomi: I was intentionally using the existing adjective (inadvertently misspelled!) not because Ruby code is plump, but because it's such a great adjective to begin with!
Java all looks the same, so there's no need for one.
Good C++ is whatever adheres to whatever coding standard you happen to be using. I'm not sure you can get much more consensus than that.
Perl and Ruby are too all over the place (TIMTOWTDI), so using a supposed adjective might actually be considered insulting or at least impolite in those communities. (i.e. there is no such thing as "idiomatic" Perl/Ruby)
There is however idiomatic Rails code, so I guess you could refer to something as "Rails-ish".
So few people in the PHP community know what good code should look like I doubt anyone could agree on what PHPleasing would mean.
I almost went so far as to say that there is no such thing as good code in PHP, but I stopped myself. There is some, but so little in proportion to all the garbage out there that it's hard to remember sometimes...
You problably know that Python has PEP.
Ok, Now, read PEP 8
"here are the Pythonic guidelines:"...
Do you know a language with a similar "open" feature ?, if yes, you should probably create adjective, verb or what you want with. Otherwhise, just pythonize your code :-)
Python is named after Monty Python. Monty Python inspired the adjective "pythonesque" which made it into dictionaries years ago.
Yet the programmers generally prefer "pythonic" over "pythonesque" when referring to the code. That's odd to me.
This is highly subjective, but can be easily understood by Pythonistas who have been with the language for awhile.
Here's some un-Pythonic code: This code is both un-Pythonic and unidiomatic. There's some code duplication which can very easily be factored out. The programmer hasn't used concise, readability-enhancing facilities that are available to him by the language. Even lazy programmers will recognize this code's clear downsides.
Here's another version that is more idiomatic but is nonetheless still un-Pythonic: Nothing about this code is particularly unidiomatic. I might even see code like this in many popular open source projects. But it's in poor taste. It's un-Pythonic.
What is the code doing? It's just taking an incoming dictionary, encoding its values using utf-8, and returning a new dictionary with those encoded values. There is no need to introduce an ItemTransformer object -- it's an extra abstraction and is just making the signal-to-noise ratio poorer. People coming from Java often write un-Pythonic code because Java is a language that does not reward good taste. The Pythonic view is: programming is hard enough -- let's not make it harder for ourselves.
Here's a more Pythonic version: This code shows comfort with Python's features, but does not abuse this comfort by obfuscating the code with mind-bending constructions. The programmer has reduced the problem to two comprehensible subproblems: creating a stream of tuples (key, encoded_value) and constructing the new dictionary from that stream. This leverages the elegant fact that in Python, dictionaries can be easily constructed from (key, value) tuples.
This version avoids code duplication while also making the last line (the return statement) a rough description of the entire function. "return a dictionary of the encoded values for the keys title and summary" Idiomatic, yes. But also tasteful, and thus Pythonic.
Even Pythonic code can be improved: And though Pythonic code is often smaller than its un-Pythonic counterparts, the experienced Pythonista knows the road to hell is paved with good intentions: Ick... time to hg revert this idiomatic Python code to the more Pythonic version ;-)
The Python community thinks about quality enough to have its own word for it.
Groovy, of course, is its own adjective.
The Pythonic version of would be
The meaning was different though ;-)
Add a comment: