views:

1716

answers:

10

I want to begin to learn Python, and I've seen that phrase come up here before, but I don't know exactly what it means. I've read some websites on Python scripting, but I don't recall ever seeing that (but I could have just glanced over it).

What exactly makes something "pythonian" or "pythonic"?

+34  A: 

Pythonic means a program conforms to the idioms of the Python programming language. Every language has its own particular idiom (as Jeff said, you can write FORTRAN in any language), but Python has a particularly strong idiom built around the idea that, "there should be one—and preferably only one—obvious way to do it". Perhaps more than most languages, Python has structures and tools that make programs much cleaner and easier to write when you use Python idioms, hence, all the interest in things being Pythonic.

Chris Upchurch
Ah, so it's just following the Python idioms. I thought it might be something deeper than that, but I guess not. Thanks.
Thomas Owens
+50  A: 
$ python
Python 2.5.1 (r251:54863, May 18 2007, 16:56:43)
[GCC 3.4.4 (cygming special, gdc 0.12, using dmd 0.125)] on cygwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import this
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!

Hope that helps.

toby
Thanks, you beat me to it... ;)
mmattax
That is precisely what I was going to wrte :-)
Bartosz Radaczyński
Have you seen the source code for "this" ? Simply brilliant!!!
@therman: I cannot decide if it is beautiful or evil!
grieve
Source of "this": http://svn.python.org/view/python/trunk/Lib/this.py?view=markup(Hey, it's the web! You can link to stuff!)
hopla
+12  A: 

in a python interpreter you can type this:


import this

which is a sort of "easter egg" that will spell out some of the python ideals...

mmattax
It's a real shame you missed some of those many up votes for the previous answer! You should have gotten at least a third of them IMHO :)
Wayne Koorts
Hahaha. I didn't know that you can actually do that.
jpartogi
+4  A: 

Here are a couple of pages that I have found to help. Code Like a Pythonista and something similar in a slideshow format.

swilliams
Oooh! Excellent slides, thanks for the link.
Beni Cherniavsky-Paskin
+2  A: 

The answers here are good, but it should also be noted that, since the definition is rather elastic, plenty of people will use "pythonic" to mean "the style I prefer". More heat than light has been generated in discussions over what style is "more pythonic", in my experience.

Allen
A: 

Python best practices -- examples of pythonic code (along the line with Code Like a Pythonista).

J.F. Sebastian
+6  A: 

You may also wish to read PEP 8 (Style Guide for Python Code), which isn't exactly about what's pythonic ("pythonic" is higher level than style), but is along the same lines.

Tony Meyer
+2  A: 

Isn't it the opposite of TIMTOWTDI ?

Dominic Cronin
Answering a question with a question?
jpartogi
Yes - but please look up the definition of recursion :-)
Dominic Cronin
+2  A: 

Guido van Rossum (the inventor of Python) is writing a blog on the history of Python, and the second article is about the philosophy.

Justin Love
+2  A: 

Pythonic for me means a programming language feature or syntax that is very concise, elegant, powerful and obvious to use. Meaning it always does what you expect it to and does not trip you up (ever).

Not all Python stuff is "pythonic", but because the language's features generally reflect this ideal, the adjective "pythonic" has come to stand for such.

Andz