Skip to main content
4 of 4
Added source fence with language code. Stylized tech name.
bad_coder
  • 12.7k
  • 2
  • 19
  • 51

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