150

On many websites I often see comments that code isn't Pythonic, or that there is a more Pythonic way to achieve the same goal.

What does Pythonic mean in this context? For example, why is

while i < someValue:
   do_something(list[i])
   i += 1

not Pythonic while

for x in list:
   doSomething(x)

is Pythonic?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jon
  • 9,815
  • 9
  • 46
  • 67
  • 15
    [The Zen of Python](http://legacy.python.org/dev/peps/pep-0020/), [What is Pythonic](http://blog.startifact.com/posts/older/what-is-pythonic.html), etc... – Martin Tournoij Jul 29 '14 at 08:28
  • This is an off topic question. You could have provided a small code sample to make it more on topic. – ρss Jul 29 '14 at 08:32

1 Answers1

166

Exploiting the features of the Python language to produce code that is clear, concise and maintainable.

Pythonic means code that doesn't just get the syntax right, but that follows the conventions of the Python community and uses the language in the way it is intended to be used.

This is maybe easiest to explain by negative example, as in the linked article from the other answers. Examples of un-Pythonic code often come from users of other languages, who instead of learning a Python programming patterns such as list comprehensions or generator expressions, attempt to crowbar in patterns more commonly used in C or Java. Loops are particularly common examples of this.

For example, in Java I might use

for (int index = 0; index < items.length; index++) {
     items[index].performAction();
}

In Python we can try and replicate this using while loops, but it would be cleaner to use:

for item in items:
  item.perform_action()

Or, even a generator expression

(item.some_attribute for item in items)

So essentially when someone says something is un-Pythonic, they are saying that the code could be rewritten in a way that is a better fit for Python's coding style.

Typing import this at the command line gives a summary of Python principles. Less well known is that the source code for import this is decidedly, and by design, un-Pythonic! Take a look at it for an example of what not to do.

informatik01
  • 16,038
  • 10
  • 74
  • 104
James
  • 3,252
  • 1
  • 18
  • 33
  • 37
    Btw, the source code for "import this" is here: https://github.com/python/cpython/blob/master/Lib/this.py – Gautam Sep 02 '16 at 14:43
  • What would be an example in a different field? Like cooking, designing, driving..? –  Oct 07 '16 at 15:18
  • 2
    For an example of some more complex code, an "unpythonic" implementation of the Vigenere cipher is made progressively more "pythonic" in this answer (disclosure: my code and opinions): https://stackoverflow.com/questions/2490334/simple-way-to-encode-a-string-according-to-a-password/46854931#46854931 – Nic Nov 08 '17 at 00:46
  • 47
    I think I should leave a comment here given my user name – Pythonic Jul 28 '18 at 15:03
  • 5
    Also perhaps worth noting that [the general term is "idiomatic"](https://stackoverflow.com/questions/84102/what-is-idiomatic-code#84270) – Louis Maddox Nov 13 '20 at 12:19
  • 2
    Thank you @LouisMaddox ;) I came here looking for this term (idiomatic) because I had forgotten the right word; and was not disappointed :) – Eric Jones Sep 06 '21 at 15:34
  • 'Pythonic' has no official meaning. It's used as a bludgeon word for 'My way is better'. 'Clear and readable' is subjective. Using a list comprehension (a feature of python) doesn't make code clear. Neither does a for loop. It's not that there is no 'best' way; it's that the word 'pythonic' is meaningless. I've had people tell me that it's 'unpythonic' to make code too concise, but others telling me to be more explicit. A better term would be 'idiomatic'. You can write idiomatically, but 'clear' and 'readable' and 'concise' are entirely in the eyes of the reader and are often in tension. – ccoffman Mar 17 '23 at 18:33