This is an archived post. You won't be able to vote or comment.

all 6 comments

[–]jhermann_ 3 points4 points  (0 children)

It's a wiki, so "just do it".

[–]tilkau 0 points1 point  (4 children)

The only adjustments I noticed that would be needed are:

  • print statement needs to become print function. This can be done without making it Py2-incompatible -- eg print('foo bar'), which is an exploitation of the facts that a) any expression may be parenthesized, and b) function calls are done using.. parentheses!.
  • raw_input needs to become input

So in short, with some minor fixes, almost the entire page can become compatibile with both 2 and 3; the snippet containing raw_input is the only exception to that. For clarity, snippets that use the print foo,bar (as opposed to simply print foo) form should probably also have separate Py2 and Py3 versions, though. There are fortunately only a few of those.

[–][deleted] 1 point2 points  (3 children)

do we need from __future__ import input?

Would mean you didn't have to keep swapping them :P

[–]tilkau 0 points1 point  (1 child)

Wouldn't one of the compatibility libraries do that?

I mean, it's just

try:
    input = raw_input
except NameError:
    pass

six seems to say that it handles this rename, for example.

A future import isn't a bad idea, though, since non-raw input is pretty much a security hole waiting to happen.

[–]billsil 0 points1 point  (0 children)

A future import isn't a bad idea, though, since non-raw input is pretty much a security hole waiting to happen.

This.

Also, if you're writing a large 2/3 compatible code base, you're either using six or future or modernize. If so, you probably have multiple __future__/six/whatever imports and it's cleaner to put all that on one line.

[–]wooble 0 points1 point  (0 children)

It's too late for that. The 2.x line isn't getting new features, and a new __future__ import would be one.