Changing the emphasis of python answers from python 2 to python 3

by QuantumChris   Last Updated October 09, 2019 15:24 PM - source

Frequently, I come across an excellent answer here, here or here, for example where the answer is written with python 2 in mind, and python 3 edited in as an afterthought.

I don't think the python 2 should be removed from answers, but the emphasis should be changed so that the first exposition of the code is python 3.

My reasons:

  • Python 2 will very shortly become unsupported. New users writing python for the first time will come across old code and may not yet know the difference between python 2 and 3. Having python 2 first indicates it is the preferred or most used version.

  • Trying the first answer should more often than not give the desired result. If someone's trying to learn a new topic, they don't want Python 2/3 differences getting in the way. Python 2 should be a "by the way, this is how we used to it" for people who are stuck on legacy code or interested in the evolution of the language. These people will be used to understanding the differences and know what they're looking for.

  • When python 3 equivalent code has been edited in after the fact, it adds confusion to have extra blocks with something such as "in python 3, just omit object" or "in python 3, the if block can be removed"; why can I remove it? which if block? This is mess that should be handled by people stuck on python 2, not everyone else who's using python 3.

  • Some differences are hard to notice and might not be picked up at first, creating confusing errors. (integer division, unicode, etc)

  • Certain changes from python 2 to 3 greatly improved readability, making it easier to interpret as a new coder. Again, this makes grasping new concepts easier.

# python 2
print 'hello %s %s' % (forename, surname)
# python 3
print(f'hello {forename} {surname}')

# python 2
class MyClass(BaseClass):
    __metaclass__ = SomeMeta
# python 3
class MyClass(BaseClass, metaclass=SomeMeta):

It seems odd that this hasn't been done already. Questions tend to follow python's style guide PEP8, clean, up-to-date code is obviously encouraged, but python 2 is everywhere in old answers.

So, is it OK to flip the emphasis on these old questions and answers? Interested to hear people's thoughts on the matter; I suspect a lot of you will just think this isn't important at all!

Related Questions

Add a way to highlight/emphasize changes in code

Updated August 11, 2017 18:24 PM

formatting c output

Updated September 26, 2019 17:24 PM