59

When is it permissible to update other people's answers for Python 3?

It's a slippery slope. But it would be such a minor change. Look at this little gem:

with open("datafile") as myfile:
    head = [next(myfile) for x in xrange(N)]
print head

Of course, xrange() is now just range(), and print is now print().

At some point in the (near) future, examples like this will no longer be relevant since new users will most likely start with Python 3 or later.

What's the plan?

I imagine an organic change will happen regardless: OPs editing their examples, adding the tag. And then maybe users updating other people's simple snippets like this gem.

Of course, the obvious problem is the breakdown of comment relatedness. The above example is a good one, because its comments refer to both Python2 and Python3, and these comments would be irrational if the code was updated.

A related meta-topic are questions of this sort, How to deal with hugely upvoted, bad and outdated answers?.

15
  • 2
  • 1
    I found another I was looking for too, Introduce an “Obsolete Answer” vote and How do we encourage edits to obsolete/out of date answers?
    – xtian
    Commented Apr 22, 2017 at 19:32
  • 3
    Maybe there should be a flag to suggest the question would benefit from an update? Then run these flagged posts to another page for users who would like to earn site points to make updates.
    – xtian
    Commented Apr 22, 2017 at 19:36
  • 1
    Related from other se unix.meta.stackexchange.com/q/4413/41104
    – Braiam
    Commented Apr 22, 2017 at 19:41
  • see When should I make edits to code?
    – gnat
    Commented Apr 22, 2017 at 20:00
  • 9
    True, there are many answers on old posts that are valid only for python2. But hey, python2 aint going anywhere till 2020. So yeah, IMO, the best way is to add a new answer for python3 and comment on the answer stating that it is valid only for python2. However if changing the print statement or only the range function, then I feel that it's better to just ask the OP to update. Suggesting an edit there would not be the right thing as the reviewers might not be aware of the language. (I have updated one myself).
    – Bhargav Rao Mod
    Commented Apr 22, 2017 at 20:34
  • 1
    "...editing is encouraged", and if no one complains, then it was a good edit? ahaha
    – xtian
    Commented Apr 22, 2017 at 20:42
  • 30
    What happened in 2009 should stay in 2009. If you have to fix then consider doing the least amount of damage by simply changing the tags on the question. [python-2.7] is a tag with lots of Q+A. Commented Apr 22, 2017 at 22:33
  • 1
    Guido van Rossum says you have to use Python3. haha. But seriously, what was a best practice for implementing some x in Django 1.x should stay in 2017. But, until there is a major overhaul of the Python language, printing line n from a text file will not change--much. And what ends up happening is new users have to learn how Python2.x has changed in Python3.x even before they learn Python3. That's awkward. Can I suggest there is a type-theory we need to consider; types of questions that rise to some meta-level of relatedness, and others more basic. No?
    – xtian
    Commented Apr 22, 2017 at 23:05
  • 54
    What "new users" start with is only partially relevant. StackOverflow isn't just for people learning to code, it's also for people who are dealing with real world legacy code bases. As much as Guido et al. might dislike it, there's still a bunch of people who are dealing with Python2 code, so rampantly converting everything to Python3 is not helpful. -- Pointing out (e.g. through comments, alternative answers) that Python2 and Python3 differ and how things would change for each is helpful, but just updating people's answers to Python3 isn't.
    – R.M.
    Commented Apr 23, 2017 at 2:31
  • 12
    In the fortran tag we have loads of questions about Fortran 77, we really don't force everyone they should update the questions and answers to Fortran 2008. And in my practice for scientific data analysis I still see way more Python 2 code than Python 3. Commented Apr 23, 2017 at 10:42
  • 1
    I'll give you my Python 2.7 when you pry it from my cold, dead hands.
    – minou
    Commented Apr 24, 2017 at 19:33
  • @R.M. Thank you for that point - as someone who a) has zilch control of the platforms, software and codes I work with and b) suffers from them being persistently and horribly out of date, it is very aggravating to ask a question (or find someone else's question) and have the only answers be "Why on earth are you using that ancient thing? Just upgrade!". Leave the Python 2 be.
    – Ajean
    Commented Apr 24, 2017 at 22:52
  • 1
    Doesn't the answer strongly depend on whether the question was tagged python-2.x, python-3.x or simply python? If it was only tagged python shouldn't we make a best-guess which version they wanted, based on the year posted, that code samples, and other Python questions they've posted? Since at least 2016, it's ok to assume python-3.x as default unless they specify otherwise, such as mentioning legacy codebase, 2.x dependencies or such. If they simply say they're a new user and do nothing to indicate 2.x, then it's acceptable to assume 3.x
    – smci
    Commented May 28, 2018 at 10:28

9 Answers 9

85

Personally, I would say that it's only permissible to change the code when no one uses any version of Python prior to 3. Until then, an additional answer for Python 3, coexisting with the Python 2 answer, would be better. (I wouldn't recommend editing Python 3 code into a pre-existing answer, because then if you make a mistake, that answer's poster takes the fall instead.) You could also comment on the answer, to let the poster know how it's done in Python 3. In cases where there are significant differences between Python 2 and Python 3, it may be better to just mark the question as a whole as being specifically relevant to Python 2, as Hans Passant said.

This is just my opinion, and I'm sure there are others who either partially or entirely disagree. I just don't like depriving the site as a whole of knowledge that can still be useful, and I don't like it when people put their words in someone else's mouth (regardless of whether the words are true, or whether it's something the person would say).

3
  • 46
    "when no one uses any version of Python prior to 3" - in other words, never. Commented Apr 24, 2017 at 16:25
  • 3
    I think adding a second answer that's just like the first is a bad idea. That just splits the points for the answer in two making them stick out less against bad answers. I think it would be better to edit the existing answer by adding additional code for python3 and marking the old code as python2. Commented Apr 25, 2017 at 11:36
  • @GoswinvonBrederlow Probably best to contact the answer's OP to do that, then, instead of editing it in yourself. Commented Apr 27, 2017 at 16:49
61

In my opinion, you are not permitted to simply change the code that has been given in the context of Python 2 being around and most popular.

There is a huge danger of decoupling questions and answers and comments from each other by doing so.

I think there are several options:

  • Provide a new answer, title it with "Solution for Python 3".
  • Comment below the answer or question, saying that this code will only be working in Python 2, because of reason X.
  • Edit the question or answer with "Note that this code runs in Python 2".
  • Edit the question or answer by providing comments in the code like
a = raw_input("ask: ")  # use input("ask: ") for python3

In any case, do not change the code.

If the question and its answer are really specific to Python 2, don't change anything, just make sure it has the tag.

When answering Python questions, make them as compatible as possible with both versions. E.g. range(N) or print(head) will work in both versions (so the example in the question may acutally be one of the very rare cases where code may be changed - however best in consent with the posts author.); where necessary simply import from __future__. When using Python 3, leave a comment in the line which might be different for Python 2.

Remember: Python 2 is not dead (yet), it is still used more often than you might think, especially in productive, educational and scientific environments from where the highest demand for quick solutions on SO arises.

To simply put some numbers into play, here is a graph from a 2016 python-developers-survey:

enter image description here

3
  • +1 @AllІѕVаиітy If you were going to edit the answer, then add a new one for Python 3. If all you were going to say is the answer isn't applicable to Python 3 (without the effort of updating the code), then just say that in a comment. Commented Apr 24, 2017 at 16:28
  • 1
    > Python 2 is not dead (yet) < with projects like Django going Python3-only, a lot less people are (and will be) using it casually. While COBOL is still used somewhere, it's not a mainstream language anymore.
    – o11c
    Commented Apr 25, 2017 at 2:31
  • 6
    @o11c I added some numbers to the post. However, my point is more that even if only 2% of python users were on python2, it's still not justified to simply change the answers. Commented Apr 25, 2017 at 7:45
33

Nothing seems to directly say this yet, so I will. The rules are simple:

  • Do not modify the Python 2 code - Python 2 is still in use, and that answer may be useful to people coming by.
  • If the question is specifically only for Python 2, do make sure it's tagged correctly, and move on.
  • If the question is not tagged specifically for Python 2, do edit the existing answer. Label the code as Python 2, but don't otherwise change it. Instead, add the appropriate Python 3 code, e.g. in this case:

Python 2 solution:

with open("datafile") as myfile:
    head = [next(myfile) for x in xrange(N)]
print head

Python 3 solution:

with open("datafile") as myfile:
    head = [next(myfile) for x in range(N)]
print(head)

This is the ideal solution, because it's additive. You haven't modified the Python 2 solution in any way. And an update to a post is entirely in line with the rules stated for when to edit:

When should I edit posts?

...

  • To correct minor mistakes or add updates as the post ages

Don't be afraid to make a valid edit on someone else's answer, as long as you're not destroying their valid code. That's the way this site works. It's much better to do that than to leave comments, which are ephemeral, or to post your own answer that is almost an exact copy of someone else's answer. This is exactly why all answers are able to be collaboratively edited.

5
  • What if there already exists a Python 3 question? Commented Apr 24, 2017 at 17:38
  • If one question was Python 2 and the other was Python 3, they're technically different (but very similar) programming languages, so the questions can be distinct. I'd probably just add a comment to the question, like related: https://thelink..., because one question may give a good answer to the other question, but each can survive on its own. If the questions aren't mutually exclusive (e.g. one is "Python 2" and the other is "Python"), I'd consider closing the more specific one as a duplicate of the less specific. Dupes aren't bad, and tightly connect both questions. Commented Apr 24, 2017 at 17:54
  • note that in this case the range(N) solution works for Python 2, so that's a particular case. it's just that it's less efficient / problematic if N is very high because of the non-generator aspect. If N=12 that doesn't matter if you use range Commented Apr 25, 2017 at 6:49
  • And if you are concerned with the waste of using range() then why would you keep all lines in memory instead of printing them directly? If N is 1000000000 then range will be the least of the problem. Commented Apr 25, 2017 at 11:44
  • What happens if the issue is not relevant to 2 or 3? ie. I remove the tags from the question to a generic one.
    – Braiam
    Commented May 15, 2022 at 14:40
7

I like both answers (1, 2), but only parts of them. The truth is, when you do a google search for a python-3 question, python-2 tagged answers still show up in the search results, often at the top. And while I agree with not changing the original answer (because it did answer the question), I do this:

  • Look to see if there is a linked Python-3 question. If found, up-vote the comment or answer it appears in, then go to the Python-3 question and up-vote it.
  • Read through all the answers and look for a Python-3 answer. If found, up-vote it.
  • If not found, and you have time, post a Python-3 answer, marked as such. If you don't have time, at least add a comment that this is Python-2 only.
  • Look for a comment in the top answer about there being a Python 3 answer below.
  • If found, up-vote it. If not found, add a comment.
  • Look for a comment on the question stating that this is a Python-2 question, and there is a Python-3 answer below.
  • If found, up-vote it, if not found, comment.

I figure that gives as much help as we can to readers looking for the Python-3 solution, without actually editing the answers. And it perhaps teaches readers to look beyond the first answer.

2
  • 5
    You say you like "both answers", which answers are you talking about? Remember, the order of the answers can change depending on the number of votes.
    – Flimm
    Commented Apr 24, 2017 at 15:47
  • Justin Time's and ImportanceOfBeingEarnest's. Commented Apr 24, 2017 at 17:31
3

The code should not be edited. If you make any edits at all to the post, it should just be something to the effect of

For Python 2.7x:

before the code sample, or perhaps add the version relevant tag to the question. If you really want, you could add a comment to the answer linking to another answer that has a 3.x solution.

If you wish to add another answer for Python 3, add another answer for Python 3 (after checking to make sure there isn't already a high quality answer out there for Python 3, of course). I see this very routinely in [xslt] related questions, where Version 1.0 is still widely used (and sometimes the only option on a given platform), even though 2.0 and 3.0 have been out for some time now. The way it's handled there is to specify which version a given answer is relevant for, and sometimes providing examples for multiple versions if it's not clear what the asker was looking for, e.g.

If you're using version 1.0, this will work: code sample for 1.0

But if you can use 2.0, here's an easier way to do it: code sample for 2.0

However, this should only be done by the original author.

You should never assume that viewers will only ever want to see the latest version, and you should never go about "translating" code in accepted answers to something else. It's frustrating to sometimes see that the most highly upvoted and accepted answer isn't relevant on a more modern platform, but that doesn't mean it won't be relevant to anyone and it doesn't mean you should be putting your translation (which may or may not have been correct/trivial) into the hand of the original answerer.

1

Just an update:

The answer was edited adding both 3 and 2 versions of the answer. It has only gotten upvotes since then. It seems that every answer here is wrong: their opinions would not have let the answer to continue to be useful until today. Everyone won in this ordeal: the answerer is still getting upvotes, the editor put good information out there and the readers feel that that answer fulfills their needs.

4
  • 1
    Good point, and in 2022 I think the case for adding Python 3 versions of answers (where this is a straightforward change to the syntax or the name of a built-in function) is stronger than it was in 2017. That said, to be strictly fair, we don't know how many upvotes the answer would have gotten if the edits hadn't been made, and "the answer was upvoted since I edited it" doesn't generally exempt an edit from following the rules (such as not changing the original author's intent).
    – kaya3
    Commented May 15, 2022 at 15:54
  • 1
    @kaya3 More important is that there's no downvotes, so at least the value was not decreased, which is what people are always worried about. In other words, at worse the edit did nothing (presuming it would have gotten the upvotes either way).
    – Braiam
    Commented May 15, 2022 at 17:41
  • 2
    I think there's a difference between editing to demonstrate an alternative over editing to change to contemporary usage. I wouldn't object too much to edits along the lines of "xrange was the Python2 way, nowadays we use range". I would be unhappy with edits that simply changed xrange to range in a code block without comment and would regard edits that changed print x to print(x) as not offering any improvement. Commented May 15, 2022 at 18:16
  • 1
    @snakecharmerb I will be unhappy with having bad information on the site.
    – Braiam
    Commented May 15, 2022 at 20:51
-1

Never

There are two ways this site can handle this,

  • Add Python2 to the question's title and add the appropriate tag. "Using Python2, how do I $foo"
  • Make a single answer for both Python 2 and Python 3. Place Python 3 at the TOP of the answer. Use markdown to differentiate.
    Python 3
    ====
    
    Today, you would do it like this,
    
    Python 2 (historic)
    ====
    
    With Python 2, you would have to do it like this,
    
    

In other news, another proposal that would be tremendously unpopular from my reading, but that I would love and support is to mass-migrate the now unmaintained Python 2 to Retrocomputing StackExchange. That where unmaintained and marketed software should go, imho. The only minor hold back with Python 2 is that ActiveState currently does market support for the platform, which makes it not totally retro. That one distribution would be on topic here.

-3

Python 2 is end of life unlike Python 3. We want to maximize the utility of questions and answers. Because of that, there are some situations I would definitely update code:

  • if it's your question,
  • if it's your answer,
  • if the answer uses deprecated style, and
  • if there's good reason to believe that an updated answer would be more useful to more people.

If the question is specifically marked as Python 2 and if the answer is the kind of thing that is more than a fun individual project, but could be used in a code that will persist, then I would consider updating answers to use six. This way, code that's written using the answer would be easy to update. six is available in Python 2 (of course), so the change is fairly transparent. For example, instead of:

from urlparse import urlparse

you just have

from six import urlparse

Doing this early will save everyone a big headache later because, for example, six makes urlparse work the way the Python 3 version works. Before you make this kind of change, make sure that your change is actually transparent.

2
  • six is quite cumbersome in the area I work with and definitely not how I would recommend writing code for either 2 nor 3 today - is crosscompatibility really worth the loss of code quality? IMO forcing Python 3 compatibility would often be a net loss of utility. Commented Nov 8, 2022 at 18:54
  • @MisterMiyagi This answer is 5 years old. Most people should not be writing code for Python 2 today. The language has been unsupported for almost three years. If you are forced to write Python 2 code, then I'm sorry, but I can't imagine why you think that six is "not recommended". It is the standard. If you think that six costs code quality then I suggest you ask a question on stackoverflow and link it here.
    – Neil G
    Commented Nov 9, 2022 at 6:33
-13

I do not edit out Python 2 information, but I will freely edit in Python 3 information.

For instance, say an answer has this code sample that only works in Python 2:

from urlparse import urlparse
    
parts = urlparse('http://docs.python.org/library/')

I will gladly modify it to make it look like this:

try:
    from urllib.parse import urlparse
except ImportError: # fallback for Python 2
    from urlparse import urlparse
    
parts = urlparse('http://docs.python.org/library/')

The new code works equally well in Python 2 and Python 3.

7
  • 12
    This would make stackoverflow much less helpful for everyone still using Python 2. I know some think everyone should be using P3, but if people regularly did what you are suggesting it would be the opposite of helpful, not just to the original post (assuming it was a P2 question), but to people who come here for simple readable, pythonic code. Downvoted b/c of this. Also, people who code in P3 can typically translate really good answers from P2 to P3. If not, just write a new answer. I beg Python 3 crusaders: don't wreck beautiful answers!
    – eric
    Commented Apr 24, 2017 at 13:32
  • 6
    Maybe it works ... but I doubt that it is good practice to write production code like that.
    – Stephen C
    Commented Apr 24, 2017 at 13:32
  • 2
    I strongly agree with the value of "simple readable, pythonic code." and _"write a new answer.". I disagree with the statement, "people who code in P3 can translate really good answers from P2 in most cases." This only applies to python users who previously used Python2 and need to make the change. The field is very broad--write a few scripts to 'automate the boring stuff' to developing Django web services. I'm starting to wonder if Python is unique in this respect. Whereas with a language like, i dunno, Fortran? we can assume you have some formal introduction to programming.
    – xtian
    Commented Apr 24, 2017 at 13:50
  • 5
    @neuronet How will this make anything less helpful for those still using Python 2? Did you even read my answer? I advocate keeping advice for Python 2 there!
    – Flimm
    Commented Apr 24, 2017 at 15:44
  • You should never write code like this. Always use six.
    – Neil G
    Commented Apr 24, 2017 at 18:11
  • Any idea of the performance hit doing this throughout your code would take? It may be more compatible, but this would be unreasonably slow for any script that does non-trivial processing. This also will make code significantly less readable... Commented Apr 25, 2017 at 13:04
  • 2
    @user2366842 no, it costs nothing. The import is only done once. But, like I said, you should just use six, which does the same thing and makes the functions more similar.
    – Neil G
    Commented Apr 25, 2017 at 20:21

You must log in to answer this question.

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