Input() in Python3
clp2 at rebertia.com
Fri Apr 22 09:46:17 CEST 2011
On Thu, Apr 21, 2011 at 11:22 PM, harrismh777 <harrismh777 at charter.net> wrote:
> My interactive scripts are giving errors on the input(). I discovered
> another fairly significant change in Python3, as discussed in PEP 3111.
> I was a little flabbergasted to discover that input() was proposed to be
> removed 'totally' from 3000. Of course I agree with PEP 3111 and am thankful
> that input() is still a built-in. doh.
Well, it pretty much *was* totally removed; it was prone to misuse and
had very few legitimate uses. It's just that raw_input() also got
What were you using it for? There are often much better alternatives.
> The problem is that the behavior was significantly changed, again, causing
> existing code to break un-necessarily.
Python 3 has many "unnecessary" backwards-incompatible changes; the
Python devs have made this abundantly clear and one should be aware of
this before going into Python 3. And again, I don't think a "behavior
change" is the best way to conceptualize this, although from a user
perspective, there indeed isn't much difference between a behavior
change and a simultaneous removal and renaming.
> So, input() used to be equivalent to:
> eval(raw_input("prompt>")) --> value
> now we get this for input():
> raw_input("prompt>") --> string
> I'm not whining or anything, just wondering why? Could someone enlighten me
> Anyway, it looks like the best fix for 2.x --> 3.x code changes:
> change: a = input("enter a number > ")
> to: a = eval(input("enter a number > "))
Did you run your scripts through the 2to3 converter tool? This is one
of the many changes it can apply automatically.
> Again, this is just another example where leaving the advertised interface
> alone would have made more sense... unless of course, I'm missing something
> important here.
input() was rarely used correctly and is quite trivially replaced.
raw_input() was used much more frequently, but was a bit awkwardly
named. Python 3 made use of its backwards-incompatible status to
rectify both of these problems at once. Writing correct code will be
now easier for newbies.
If you're porting stuff to Python 3, using 2to3 and reading the
summary of changes from 2.x are absolute necessities.
More information about the Python-list