23

Often people ask for language specific versions of some code snippets. For example a term that seems to be found rather frequently in questions with a python tag is "pythonic" and people ask for pythonic version of some (often already working) code.

Personally I never considered this type of question as being prone to lead to very opinion-based answers, as probably all programming languages have idiomatic ways to write code and in their communities there is often a rather precise concept of what is specific to that language.

However, recently I came across the following question:

"Pythonic" for loop over integers 0 to k-1 except i

where what is asked should be rather clear from the title already. I was surprised that the question is put on hold for being primarily opinion-based. Thus my questions:

  • Was this the right decision?

  • Should we generally flag question asking for idiomatic code snippets with primarily opinion-based?

9
  • 7
    FWIW, I think asking whether working code is idiomatic/how to make it idiomatic is better suited to codereview.se, but I don't think it should be closed as "opinion-based". The linked question (at a glance) seems more like there happen to be multiple valid solutions, which shouldn't be inherently bad.
    – Michelle
    Commented Aug 21, 2014 at 15:26
  • 1
    So, since you feel the question isn't opinion based, how can you concretely determine what the best answer is, or even whether a given answer concretely answers the question?
    – Servy
    Commented Aug 21, 2014 at 15:29
  • 1
    @Servy Well, what is idiomatic in a language can be determined. Since that there are often many different versions to write a simple task as the one asked in the example, we can judge them by comparing how language specific they are.
    – j-i-l
    Commented Aug 21, 2014 at 15:43
  • 4
    There is more than one way to skin a rabbit and the way that is best is based on the opinion of the skinner. Commented Aug 21, 2014 at 15:44
  • @jojo And what are those objective metrics? What, other than just your opinion, is the basis for determining whether or not an answer is more or less pythonic? All your comment says so far is that you're capable of providing an opinion on which answer you like more.
    – Servy
    Commented Aug 21, 2014 at 15:46
  • 1
    @Servy: I'm not a linguist so I don't know about metrics to measure dialects. but I guess that if you would talk to one and tell him that dialects are basically based on personal opinions... well I guess he will not like you.
    – j-i-l
    Commented Aug 21, 2014 at 15:54
  • @jojo A given block of text either is using a given dialect or it is not. It is a boolean consideration. Much like a given code snippet either is valid python or it is not; it is a boolean test. It is not a sliding scale. For a linguist a given block of text either is English or it is not English. You don't ask them which of two blocks of text are "more English". Each one either is, or is not.
    – Servy
    Commented Aug 21, 2014 at 16:01
  • 1
    On Meta.SE: Are "pythonic" questions opinion-based?
    – jscs
    Commented Aug 21, 2014 at 18:04
  • @derp Technically it's not valid English. "more" is used to qualify something with a discrete scale of degree. English has no such scale, since it is a strictly boolean quality, thus the term "more English" is not actually grammatically valid.
    – Servy
    Commented Aug 21, 2014 at 18:10

4 Answers 4

23

Pythonicity

Pythonistas take pride in the Pythonicity of their code. PEP 20 is a manifesto of "Pythonic" principles:

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

The language is designed so that there are usually one (or just a few) good way to express the code (#13), and the culture of the Python community embraces that. I might go as far as to say that every question about how to write something in Python carries an implicit desire for Pythonicity.

Code Review

Speaking as a moderator for Code Review…

Code Review is a Stack Exchange community that helps programmers improve their code. On Code Review, we promote many of those principles listed above. Therefore, it may be tempting to refer more-idiomatic-version requests to Code Review.

However, before you even think about referring users to Code Review or flagging questions for migration, please take care to consider whether the question is actually on-topic for Code Review, as we have rather stringent requirements for questions.

Since many migrations from Stack Overflow to Code Review are rejected, we have prepared a checklist for migration. Many of the same criteria would also apply to cross-posting.

The cited question would be a poor question for Code Review because it is a completely generic how-to question. Code Review critiques actual code from a real project, in its natural habitat, so that we can offer custom-tailored advice.

It wouldn't take much of a change to make the question on-topic for Code Review, but that initiative has to come from the author. Completing the for-loop to make it working code, and providing a realistic motivating context would suffice.

Stack Overflow

As I noted above, the characteristics that make code Pythonic are laid out in PEP 20, so asking for Pythonicity is "good subjective", not primarily opinion-based. As evidenced by the current leading answer, there is indeed a Pythonic way to write it:

from itertools import chain

for j in chain(range(i), range(i+1, k)):
    # ...

Since every Python question inherently seeks Pythonicity, it would be nonsense to close all such questions on Stack Overflow as primarily opinion-based.

Furthermore, you could also retitle and strip down the question to

For loop over integers 0 to k-1 except i in Python

I want to create a for loop that will go through the integers 0 to k-1, except for integer i. (I'm comparing some lists of k items, and I don't need to compare item i in one list with item i in another list.)

That question is clearly on-topic for Stack Overflow. It would be essentially an identical question. The responses would be exactly the same as the current ones. It makes no sense that adding a bit of code and text to clarify the request would make it off-topic for Stack Overflow.

3
  • 1
    I wish I could give you a bounty for this answer.
    – Mansueli
    Commented Aug 21, 2014 at 19:25
  • 4
    I agree with much of your answer, but all of the principles in your manifesto are highly subjective, and saying that you're proud of them doesn't make them any less so. Despite your protestations to the contrary, asking for Python code and asking for "pythonic" code are not at all the same thing.
    – Robert Harvey Mod
    Commented Aug 22, 2014 at 20:08
  • 3
    Further, while I believe that "pythonic" is just a special case of "idiomatic," I suspect that you do not, and therefore you haven't really addressed the issue of idiomatic code, in the general sense. Many of these values are highly valued and prized by non-Pythonistas; nevertheless, few of them will argue that "beautiful", "readable" and similar subjective attributes form a suitable basis for a good Stack Overflow question.
    – Robert Harvey Mod
    Commented Aug 22, 2014 at 20:11
9

The words "pythonic," "idiomatic" and "elegant" are just fancy ways of saying "best practice."

While I think the use of the word "pythonic" is legitimate if both parties to the communication fully understand what that term means, it does concern me that folks who don't understand the term might simply throw the term about, in hopes of provoking a discussion about prettifying code.

Using the term "pythonic" makes the Python tag community insular, if you don't explain what you think it means within the context of your question or answer. In other words, why do you think the code in your question is not pythonic, and why do you think your answer is more pythonic? Otherwise, it's only useful to the asker and answerer (and maybe others in the Python tag). You might as well be using SO as an instant messaging system at that point.

That said, I've seen several such questions, and they always seem to work out well, for whatever reason. Maybe "pythonic" is defined well enough in the Python community that unconstructiveness never arises.

Note that, in the question you linked, neither the asker nor the answerer explained why they thought their version of code was pythonic or not pythonic so, regardless of the outcome of the question, for folks like me whose first language is not Python, the post didn't really advance my understanding of what "pythonic" is, or why it is important.

3
  • Thanks for the constructive answer! So you would suggest to generally flag such questions as opinion-based then?
    – j-i-l
    Commented Aug 21, 2014 at 16:05
  • I would not have closed your example question, but maybe the other mods have a different opinion about it. I've asked in the Teacher's Lounge for additional review.
    – Robert Harvey Mod
    Commented Aug 21, 2014 at 16:06
  • 1
    -1 for not quoting :) "While you may be an exemplary, clear-thinking, individual, who uses the term 'best practice' in a constructive manner, you have been preceded by a giant procession of zombies who use it as the antithesis of thought. Instead of understanding the important specifics of their situation and looking for an appropriate solution, all they want is to spot a herd in the distance and go trotting off after it. Thus, the term 'best practice' has been rendered an extremely strong signal of a empty resonant cavity..."
    – gnat
    Commented Aug 21, 2014 at 16:20
4

None of the SO users that voted to close the question have the [python] tag anywhere near their top tags.

They do however use the kind of languages where [loops] and [for-loop] are on topic. Well, don't they all. And therefore got this question on their personalized front page. With, of course, little patience with why you would fret about something so simple just for style-sake.

Just a case of bad tagging by the OP.

Lesson learned here: be specific when you pick your tags. Don't yell for help in a crowded football stadium when all you need is a bag of popcorn.

0

I believe that that question is definitely an opinion based question. The question is asking for a more "pythonic" way of doing something. In this case we can reword the question to

What is a better way of writing this in python?

or

What is the best way to write this in python?

which is much more obviously opinion based.

All that being said, the appropriate response is probably to migrate it to CodeReview.

6
  • I agree with @derp. it becomes opinion based once you replace idiomatic with better.
    – j-i-l
    Commented Aug 21, 2014 at 15:58
  • 1
    @derp How would you define "more idiomatic" then? There isn't IMO an objective way of defining that a certain piece of code is "more idiomatic". How "idiomatic" something is IS a value judgement, thus the question is opinion based.
    – Becuzz
    Commented Aug 21, 2014 at 16:04
  • @derp So you won't ever find people who disagree on what the more idiomatic solution to a problem is. Every python programmer in the world agrees exactly on what is and is not idiomatic?
    – Servy
    Commented Aug 21, 2014 at 16:10
  • 4
    Thanks for considering Code Review as a target for this question. As it happens, the specific question about 0 to k-1 except i is not a good candidate because the question is asking "what is best practice" rather than "is this code following best practice". Also, we require that code is real code, and gives some context, so that, on Code Review, we have the opportunity to say things like: If you do this in a different way X, you can avoid that loop entirely.
    – rolfl
    Commented Aug 21, 2014 at 16:26
  • 1
    @rolfl Good to know. I'm not on Code Review much so I wasn't sure if it fell into the OK best practices category or the not OK one.
    – Becuzz
    Commented Aug 21, 2014 at 16:28
  • @derp For most questions you can generally get very wide acceptance on whether or no an answer answers the question, for questions that are not themselves unclear. The opinions come in how good of an answer it is, even if it technically answers the question. For this question one cannot evaluate, without opinion, if it is a valid answer to the question.
    – Servy
    Commented Aug 21, 2014 at 17:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .