Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #61720 > unrolled thread
| Started by | stephen.boulet@gmail.com |
|---|---|
| First post | 2013-12-12 06:45 -0800 |
| Last post | 2013-12-13 02:17 +1100 |
| Articles | 5 — 4 participants |
Back to article view | Back to comp.lang.python
input() on python 2.7.5 vs 3.3.2 stephen.boulet@gmail.com - 2013-12-12 06:45 -0800
Re: input() on python 2.7.5 vs 3.3.2 Chris Angelico <rosuav@gmail.com> - 2013-12-13 01:53 +1100
Re: input() on python 2.7.5 vs 3.3.2 Amit Saha <amitsaha.in@gmail.com> - 2013-12-13 00:56 +1000
Re: input() on python 2.7.5 vs 3.3.2 Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-12-12 15:04 +0000
Re: input() on python 2.7.5 vs 3.3.2 Chris Angelico <rosuav@gmail.com> - 2013-12-13 02:17 +1100
| From | stephen.boulet@gmail.com |
|---|---|
| Date | 2013-12-12 06:45 -0800 |
| Subject | input() on python 2.7.5 vs 3.3.2 |
| Message-ID | <2ba69b81-99f9-40d6-a337-afb2f892ecff@googlegroups.com> |
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
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
[toc] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2013-12-13 01:53 +1100 |
| Message-ID | <mailman.3996.1386860018.18130.python-list@python.org> |
| In reply to | #61720 |
On Fri, Dec 13, 2013 at 1:45 AM, <stephen.boulet@gmail.com> wrote:
> 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
>
> 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
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
[toc] | [prev] | [next] | [standalone]
| From | Amit Saha <amitsaha.in@gmail.com> |
|---|---|
| Date | 2013-12-13 00:56 +1000 |
| Message-ID | <mailman.3997.1386860214.18130.python-list@python.org> |
| In reply to | #61720 |
On Fri, Dec 13, 2013 at 12:45 AM, <stephen.boulet@gmail.com> wrote: > 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. -- http://echorand.me
[toc] | [prev] | [next] | [standalone]
| From | Mark Lawrence <breamoreboy@yahoo.co.uk> |
|---|---|
| Date | 2013-12-12 15:04 +0000 |
| Message-ID | <mailman.4000.1386861007.18130.python-list@python.org> |
| In reply to | #61720 |
On 12/12/2013 14:56, Amit Saha wrote: > On Fri, Dec 13, 2013 at 12:45 AM, <stephen.boulet@gmail.com> wrote: >> 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." -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2013-12-13 02:17 +1100 |
| Message-ID | <mailman.4002.1386861476.18130.python-list@python.org> |
| In reply to | #61720 |
On Fri, Dec 13, 2013 at 2:04 AM, Mark Lawrence <breamoreboy@yahoo.co.uk> wrote: > 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
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web