views:

897

answers:

6

exact duplicate: http://stackoverflow.com/questions/58968/what-defines-pythonian-or-pythonic

I have no python experience at all. Help me to understand this term. Please provide code examples and explanations as to what makes a particular sample "pythonic" and why.

+32  A: 
$ python
Python 2.5.3 (r253:67856, Dec 19 2008, 16:52:52) 
[GCC 4.0.1 (Apple Computer, Inc. build 5363)] on darwin
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!
stesch
I was going to post this. Good easter egg of sorts in Python.
Alex
The perfect answer.
Brian Clapper
Although these principles apply to many languages, not just Python.
JW
does it remind a little bit prolog?
pablito
didn't get it...
Paulo
+2  A: 

I'd say Pythonic is another word for idiomatic Python usage

gumuz
+5  A: 

There's one thing that newer python programmers seem to have difficulty coming to terms with: duck typing. Duck typing isn't quite the same thing as dynamic typing. For example, suppose I have an object x that I want to do something with. Many developers coming from statically typed languages will be tempted to do this:

if isinstance(x, someclass):
    x.somemethod()
else:
    print "an error has occurred!"

The problem with this code is that Python has no concept of interfaces. So if you want to create an object that can pose as an instance of someclass without directly inheriting it, you're SOL. You can also do this:

if hasattr(x, somemethod):
    x.somemethod()
else:
    print "an error has occurred!"

But this can be tricky because python's dynamic typing can allow you to call a method even if it's not explicitly set to an attribute (see getattribute for more info).

There's an alternative way though:

try:
    x.somemethod()
except AttributeError:
    print "an error has occurred!"

Though this use of exceptions will probably horrify the .net programmers out there, this is the most pythonic way to do the code. It also just so happens to be the only way that you can be 100% sure that x has or doesn't have a somemethod method.

Jason Baker
It is also a pain for the IronPython developers to deal with since exceptions on the CLR are expensive, unoptimised routines which can be slow.
BrianLy
this bare except is really ugly, you should use: except AttributeError:
nosklo
I changed the catch-all except: statement to only catch AttributeError, since it's generally considered bad to accidently catch errors you're not properly handling
dbr
Thanks for fixing that.
Jason Baker
+2  A: 

It is an inverse measurement for the amount of flak you will take when posting python code to a widely read public forum. In other words, the less pythonic the more flak.

schickb
+2  A: 

See PEP ("Python Enhancement Proposal") 8 and Code Like a Pythonista for some recommendations to make your Python code look idiomatic (Pythonic). They include formatting recommendations, naming conventions, and code idioms.

Doug