input() on python 2.7.5 vs 3.3.2

S

stephen.boulet

Can someone explain? Thanks.

Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:06:53) [MSC v.1600 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.Hello there

Python 2.7.5 (default, May 15 2013, 22:43:36) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.Hello there
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 1
Hello there
^
SyntaxError: unexpected EOF while parsing
 
C

Chris Angelico

Can someone explain? Thanks.

Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:06:53) [MSC v.1600 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.Hello there

Python 2.7.5 (default, May 15 2013, 22:43:36) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.Hello there
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 1
Hello there
^
SyntaxError: unexpected EOF while parsing

It's very simple: The input() function in Python 2.x is a very
dangerous one - it's equivalent to eval(input()) in Python 3. The
equivalent function in Python 2 is called raw_input. For safety and
compatibility, just do this at the beginning of your interactive
session or the top of your script:

input = raw_input

or, in a way that'll work in Python 3 as well, with no changes:

try:
input = raw_input
except NameError:
pass

After that, you can safely call input() and get back a string.

ChrisA
 
M

Mark Lawrence

Can someone explain? Thanks.

Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:06:53) [MSC v.1600 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
x = input() Hello there
print(x)
Hello there

In Python 3, input() considers an input as a string and returns the
input as a string. This is the behavior of raw_input() in Python 2.
Python 2.7.5 (default, May 15 2013, 22:43:36) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
x = input()
Hello there
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 1
Hello there
^
SyntaxError: unexpected EOF while parsing

In Python 2, input() expects valid Python as it's input. If you
provide your input as 'Hello there' (a Python string), it won't
complain.

HTH,
Amit.

I much prefer Chris Angelico's response "The input() function in Python
2.x is a very dangerous one - it's equivalent to eval(input()) in Python 3."
 
C

Chris Angelico

I much prefer Chris Angelico's response "The input() function in Python 2.x
is a very dangerous one - it's equivalent to eval(input()) in Python 3."

Just to clarify: If you *want* eval, then you know you want it, and
you are (or should be) aware of its dangers. The problem, imo, is
hiding something as powerful and dangerous as code evaluation behind
the innocuous name "input". If I were coding a Python 2.x REPL, I
would probably write eval(raw_input()) rather than input(), just for
clarity.

But I'm more likely to just code for Python 3 anyway. :)

ChrisA
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top