DEV Community

loading...
Cover image for In Your Opinion, What Makes Code Pythonic?

In Your Opinion, What Makes Code Pythonic?

Sean
Hi, name's Sean.
・1 min read

What does it mean, Pythonic?

What do you think makes code pythonic, write about it in the comments.

Discussion (15)

Collapse
rafaacioly profile image
Rafael Acioly

The code that doesn't make me think to understand, when i read code that look like a text in english i consider it pythonic :)

Collapse
seanolad profile image
Sean Author

So, super readable, got it

Collapse
dcsan profile image
dc

little things like list comprehensions rather than loops or maps.
using the unfortunate lambda syntax.
slavish adherence to PEP8 even when you really don't like it.

Collapse
seanolad profile image
Sean Author

Why, list comprehensions though?

Collapse
rhymes profile image
rhymes

because they are pythonic :D ?

Collapse
patarapolw profile image
Pacharapol Withayasakpunt • Edited

I followed effectivepython.com/ but now I am moving away from Python toward JS, considering thing like JSPerf, ESLint, Prettier.

  • Yes, there is still debate whether to follow Airbnb / Google / Standard...
  • I also consider Functional and Immutability. I also heard about pure functions and side effects.

But Python does have PyLint, though. But, PyCharm seems to guide me towards PEP8 better (than VSCode.)

Collapse
seanolad profile image
Sean Author

Yeah, I've been trying to write pure functions, especially while doing any form of problem solving or functional programming, and thanks for the tip about pycharm.

Collapse
ezzy1337 profile image
Derek D.

I had to tackle this same question while writing a Pythonic Guide to SOLID Design (link below). The entire article might be helpful but in short.

"Above correct syntax Pythonic code follows the normally accepted conventions of the Python community, and uses the language in a way that follows the founding philosophy."

dev.to/ezzy1337/a-pythonic-guide-t...

Collapse
seanolad profile image
Collapse
oklandon profile image
Landon Young

Use comprehensions (dict, list, etc) when possible. Although it's hip to use immutable structures and other FP approaches right now in programming, it's often more 'pythonic' to use simple imperative patterns and/or mutation since it'll be easier for other maintainers and readers to grok.

the zen of python: python.org/dev/peps/pep-0020/

Collapse
Sloan, the sloth mascot
Comment deleted
Collapse
rhymes profile image
rhymes • Edited

Actually, if we want to be super strict, filter, map and lambda are few of those things that aren't considered really Pythonic. Guido van Rossum wanted to remove all of them in Python 3, but then backtracked and only removed reduce. See The fate of reduce() in Python 3000 (dated 2005!!).

A more "pythonic" way to write that would be:

[w for w in words if len(w) == max(len(x) for x in words)]

or with a generation expression to avoid consuming all items eagerly:

(w for w in words if len(w) == max(len(x) for x in words))

or with a set comprehension to remove duplicates automatically

>>> words = ["a", "ab", "abcd", "b", "abcd"]
>>> [w for w in words if len(w) == max(len(x) for x in words)]
['abcd', 'abcd']
>>> {w for w in words if len(w) == max(len(x) for x in words)}
{'abcd'}
Collapse
bloodgain profile image
Cliff

Pythonic is just the community jargon for idiomatic. Are you following the idioms of Python?

In programming terms, an idiom is like a design pattern, only smaller or more abstract. For example, using a list comprehension rather than a generator function in Python. In C++, using the RAII principle to manage memory instead of manually allocating and deallocating is idiomatic.

Note that in Python, idiomatic code tends toward better readability. Even list comprehensions, which are a bit more opaque than the usual Python, could be deduced quite readily by an experienced coder, even if they aren't familiar with the concept. This is not necessarily the case with all languages. Like design patterns, they are just something a fluent user of the language will recognize without having to analyze it. For a linguistic example, the meaning of the phrase "let's call it a day" would not be obvious to someone just learning English, but any fluent speaker would know it means "let's stop working (on this) today/for now and come back to it later".

Collapse
shadowtime2000 profile image
shadowtime2000

When instead of taking this:

[
{"k": "v"},
{"k": "k"}
]

you take this:

the_function(["k", "k"], ["v", "k"])
Collapse
thehidden1 profile image
Mohamed Amine

snake_case ?

Forem Open with the Forem app