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.
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.
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.
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:
[wforwinwordsiflen(w)==max(len(x)forxinwords)]
or with a generation expression to avoid consuming all items eagerly:
(wforwinwordsiflen(w)==max(len(x)forxinwords))
or with a set comprehension to remove duplicates automatically
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."
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".
Top comments (15)
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 :)
So, super readable, got it
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.
Why, list comprehensions though?
because they are pythonic :D ?
I followed effectivepython.com/ but now I am moving away from Python toward JS, considering thing like JSPerf, ESLint, Prettier.
But Python does have PyLint, though. But, PyCharm seems to guide me towards PEP8 better (than VSCode.)
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.
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/
Actually, if we want to be super strict,
filter
,map
andlambda
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 removedreduce
. See The fate of reduce() in Python 3000 (dated 2005!!).A more "pythonic" way to write that would be:
or with a generation expression to avoid consuming all items eagerly:
or with a set comprehension to remove duplicates automatically
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...
Thanks
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".
When instead of taking this:
you take this:
snake_case ?