34

Today I asked this question about which of two solutions was more idiomatic in Python. It received a few downvotes, and was put on hold as being an opinion-based question.

This confused me, as being idiomatic doesn't seem to be a matter of opinion, especially in Python. Even the documentation explicitly lists a few idioms and anti-idioms here, and mentions others elsewhere like EAFP.

It would seem that these kinds of questions can be answered with references to official documentation, avoiding any issue of opinion.

6
  • 1
    Well, I suppose that, if the question can be answered with references to official documentation, it shows no research effort, is unclear why the docs are not sufficient, and so not a useful question. Downvotes would therefore be an alternative, (though I would consider both as an appropriate response too).. Nov 5, 2018 at 4:03
  • 9
    Just because it was pun on hold as an opinion-based question does not necessarily mean it is an opinion-based question. Your question is ,in my opinion, above average from a quality perspective. As advice, try not including idioms such as more Pythonic in your questions, since they have the tendency to trigger the flag police (regardless of the actual content of your question).
    – user10367961
    Nov 5, 2018 at 12:42
  • 2
    I have rather limited Python experience, but FWIW in other languages I've seen, idiomatic code may not really matter for what you do on a daily basis. I remember spending a lot of time learning how do things "the C# way" and I ended up maintaining and writing code for a team that never heard of any of it and seemed to prefer "the C way". What may be a better use of your time is spending some time on codereview, read posts on there and if you have a specific working, real-world code portion you're worried about, ask for a review.
    – jrh
    Nov 5, 2018 at 13:05
  • 7
    The majority of questions on what's idiomatic, in different languages, can not be answered with references to official documentation, because either there is no "official" documentation or it says nothing on the topic. Python may be an exception to this, but expect knee-jerk votes regardless. Remember also that the vote is about whether the question is likely to attract wholly opinionated answers -- not whether or not you've added sufficient disclaimers to prevent it. Even when a non-opinionated answer is possible, as a whole a question might still warrant closure. Nov 5, 2018 at 14:14
  • 16
    The question is off-topic. A more appropriate place to ask this question is Code Review. Its help entry What topics can I ask about here? lists the following, among others: "Best practices and design pattern usage". That's what you have been asking for. Nov 5, 2018 at 19:54
  • 3
    As a side note, thank you for asking on Meta when you're unsure if something is on-topic. If you're confused beforehand, you should probably ask here first, but realizing you may have made a mistake and asking here afterwards is just about as good.
    – Nic
    Nov 6, 2018 at 1:09

4 Answers 4

13

I've come to questions similar to the one you asked when I was first writing stuff that other people would eventually have to maintain using Python, which makes you pretty conscious of things like that. Hence, on the one hand, you have some perfectly valid decision anxiety and it's natural to want input.

So, I tried to re-word your question the most objective way that I could think of, and this is what I came up with:

Under what maintainability circumstances would eliminating elif from a long list of conditionals be appropriate?

And even then, it's a discussion of style, guidelines set out by the project maintainers, needs for scripts to generate code on-the-fly, etc. The answer is still going to be 'it depends'. The answer to are these approaches functionally equivalent? is certainly going to be yes, but you knew that.

It's not a bad question, it's just not something that anyone except for whoever ultimately decides what code gets checked in and what style people should follow could answer with any authority.

That doesn't necessarily end with idioms being on-or-off topic for anything blanketly, it just means that it's often a matter of preference unless the question is specifically asking if two things are functionally identical.

In the context of performance these are usually just as problematic. For instance, "which way is faster?" is going to result in someone saying "well, I'm not sure, did you time each and see?" That leaves "WHY is this way faster than the functionally identical other idiomatic way? as usually pretty clearly on-topic, but most questions like that have been covered.

These are great discussions to have with people you're working with, but they don't tend to make great questions for highly objective Q&A. I'd like for us to be able to support them better, somehow, but we've yet to come up with an idea on how (well, beyond chat).

35

The essence of your question is effectively this:

In a loop, should I use elif or continue?

There's no concrete answer to this - idiomatic or otherwise - since the code works in both contexts.

Put in another light, you're asking us if this code is readable to another person. I have an opinion on its readability and I'm sure others would too - some may even differ from my own.

The fact that an opinion can be drawn about the readability of this code should inform you that this is opinionated and would not be able to get a concrete answer.

20
  • 10
    Regarding "The fact that an opinion can be drawn about the readability of this code should inform you that this is opinionated" I can form an opinion about the readability of any piece of code, that doesn't seem relevant to determining whether the code follows well established idioms.
    – shayaan
    Nov 5, 2018 at 3:49
  • 1
    A possible concrete answer would be "Using continue in this context is not idiomatic, as per the documentation at www.exampledocumentation.com"
    – shayaan
    Nov 5, 2018 at 3:57
  • 2
    @Shayn: Idioms are for people who wish to debate the quality and readability of code. The code works so the code doesn't really care that it has continues all about it.
    – Makoto
    Nov 5, 2018 at 3:57
  • 6
    PEP 8 is the only really authoritative source on things like this for Python, and even then that's still largely an opinion. You don't have to follow that, it's just that people who see your code afterwards will thank you.
    – Makoto
    Nov 5, 2018 at 4:01
  • 8
    Just because the code works in both cases this does not mean that both options are equal. There are many ways to do things in Python but "There should be one-- and preferably only one --obvious way to do it." (Zen of Python) . Anything that is enshrined in the Python docs or a PEP or improves basic programming principles (e.g. DRY) is worth answering. Nov 5, 2018 at 12:49
  • 1
    A high voted Q&A based on PEP8 stackoverflow.com/questions/119562/… Nov 5, 2018 at 12:50
  • 1
    Another one stackoverflow.com/questions/15011367/… Nov 5, 2018 at 13:00
  • 4
    @Chris_Rands: Again, nothing in PEP 8 is mandatory. Even The Zen of Python is opinionated. Also your first example is actually a valid question; Python makes use of whitespace as syntax and mixing whitespace types can lead to confusing errors. Your second example is also a reasonable question; instead of asking what's the best practice, they're asking what the difference is between the two approaches and are using PEP 8 as a reference.
    – Makoto
    Nov 5, 2018 at 15:36
  • 1
    For the OP's question, the 2 code snippets are not identical, they result in different CPython byte code (and therefore presumably marginally different performance)- are you saying if the OP edits their question to "In a loop, what is the difference between use elif or continue?" it becomes on-topic? Nov 5, 2018 at 15:45
  • 2
    @Chris_Rands I would argue that both of the questions you linked are a bad fit for SO, especially the first one - just look at the top answers which are basically just "I'm doing this because it works best for me". PEP8 is an opinion, and some parts of it are far from universally accepted (e.g. insistence on 72/80 chars per line). So asking "what's better" is asking for opinions, and technically, asking "what's sanctioned by the official docs or the PEPs" is a request for an external resource, so both are off topic.
    – l4mpi
    Nov 5, 2018 at 15:46
  • 1
    @l4mpi The Zen of Python is opinionated, but it's still useful, this is my point really, many helpful SO questions are at least partly opinion-based and remain open. What is "opinionated" vs "clearly objectively better" has a rather large grey area, it feels like once we approach micro-optimisations and readability it becomes opinion based but this seem murky to me, and the OP may not know in advance if 2 approaches are clearly different or rather similar Nov 5, 2018 at 15:53
  • 3
    If readability was always subjective then code obfuscation wouldn’t be a thing Nov 5, 2018 at 16:04
  • 3
    @Chris_Rands readability is subjective. Unreadability might be more universal universal but people code in, quite frankly, languages that are stupidly opaque for the sake of being opaque like Malbolge, Whitespace, Brainfuck, and even that orangutan language (never bothered to look up the name but all instructions are variations of Ook). People use those out of their own volition and for fun. Clearly, how unreadable something is would also be subjective.
    – VLAZ
    Nov 6, 2018 at 13:57
  • 1
    There are 542 questions tagged pep8 (a tag which has existed since 2012), with 7 questions >100 score, and the blurb Questions tagged as pep8 should relate to how to apply these guidelines to your code. It's safe to say there's some interest in having this tag remain.
    – jpp
    Nov 6, 2018 at 17:51
  • 1
    @jpp The rules were more lax in 2012, but they've been tightened up because of the influx of new users. Interest does not imply on topic, many upvotes does not imply on topic, and almost certainly many of those pep8 questions should be closed as off topic.
    – Ian Kemp
    Nov 7, 2018 at 12:34
15

In general, questions about idioms should be on topic. You have some Python examples. In C++, I think it would be wrong for questions about how to do something using RAII to be off topic.

However, that does not mean all such questions are on topic. Asking for the idiomatic way of doing something where there is no consensus on the idiom.

4
  • 5
    I agree with the first part, but the second part is problematic, as the "on-topic-ness" of a question shouldn't depend on the answer. Assumedly the one asking the question don't know if there is a consensus answer (or they wouldn't ask), and thus don't know if the question is off-topic or not. The answer when there is no consensus would seem to be "There is no consensus about the idiom; as indicated by XX preferring X and YY preferring Y, or ZZ stating that both can be used." - other answers are off-topic as opinion-based. Nov 5, 2018 at 13:29
  • 8
    @HansOlsson I think in practice the "on-topic-ness" of a question often does have to depend on the answer, non-ideal though that is. Sometimes a question turns out to be too broad because a problem the asker thought was trivial would actually take years to solve. Sometimes a question turns out to effectively be a resource request and draw lots of spam answers because what the OP is asking for can only be practically achieved with a paid third party tool, even if the OP didn't know that. Such outcomes are doubtless a bit unfair on the OP, but probably still correct.
    – Mark Amery
    Nov 5, 2018 at 17:10
  • @MarkAmery I think your comment would better be formatted as an answer. Or maybe edited into this answer.
    – anatolyg
    Nov 5, 2018 at 17:17
  • 1
    As a counter-example, I see some horribly non-idiomatic code from newbies asking for homework help. I like @HansOlsson's comment - sometimes the on-topic-ness of an "is this idiomatic" question really depends on whether there is a clear answer. Learning the idiom is part of learning to write code in a language.
    – Flydog57
    Nov 6, 2018 at 5:05
1

Different communities function differently

We need to be more open-minded. It's tempting, as a possibly logical bunch of people, to write a set of rules and apply it indiscriminately across all posts. Of course, that's the principle behind reviewing. And there'd be chaos if all reviewers openly refuted such a policy.

However: closing any post including a vague term such as "Pythonic" for or "Pandonic" for isn't the way forward. These terms / ideas are here to stay. Because the world doesn't revolve around StackOverflow, but real life problems where code maintainability and performance are serious programming issues.

Many real-world problems fit somewhere between SO and CodeReview. We expect people to go down one of the two routes to channel both questioners and answerers towards solutions and problems they appreciate. But many good questions on SO have multiple and yet very different, sometimes radically different, good answers. So we cannot assume that a good Q&A is never opinion-based in the true sense of the phrase.

See, for example: Is it OK to ask a question looking for better ways to do things? Possibly every answerer on would love questions to be phrased as this one, as opposed to the give-me-the-code questions which now dominate the tag. It is, though, fundamentally opinion-based. Yes, expectations have been lowered from SO's documented aims. But we know that many Pandas users are not programmers, enthusiast or professional, and it's impossible to fight the tide. This doesn't stop a Q&A being useful, even if the target audience is less exclusive.

1
  • 4
    Closing questions is more fun than being open-minded, because we are on SO where everybody hates questions, right?
    – GOTO 0
    Nov 6, 2018 at 13:58

You must log in to answer this question.

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