Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #26394 > unrolled thread

consistent input() for Python 2 and 3

Started byUlrich Eckhardt <ulrich.eckhardt@dominolaser.com>
First post2012-08-02 11:49 +0200
Last post2012-08-02 13:07 +0200
Articles 2 — 2 participants

Back to article view | Back to comp.lang.python


Contents

  consistent input() for Python 2 and 3 Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> - 2012-08-02 11:49 +0200
    Re: consistent input() for Python 2 and 3 Philipp Hagemeister <phihag@phihag.de> - 2012-08-02 13:07 +0200

#26394 — consistent input() for Python 2 and 3

FromUlrich Eckhardt <ulrich.eckhardt@dominolaser.com>
Date2012-08-02 11:49 +0200
Subjectconsistent input() for Python 2 and 3
Message-ID<72vpe9-kki.ln1@satorlaser.homedns.org>
Hi!

I'm trying to write some code that should work with both Python 2 and 3. 
One of the problems there is that the input() function has different 
meanings, I just need the raw_input() behaviour of Python 2.


My approach is to simply do this:

   try:
       # redirect input() to raw_input() like Python 3
       input = raw_input
   except NameError:
       # no raw input, probably running Python 3 already
       pass


What do you think? Any better alternatives?

Uli

[toc] | [next] | [standalone]


#26402

FromPhilipp Hagemeister <phihag@phihag.de>
Date2012-08-02 13:07 +0200
Message-ID<mailman.2870.1343905697.4697.python-list@python.org>
In reply to#26394

[Multipart message — attachments visible in raw view] — view raw

On 08/02/2012 11:49 AM, Ulrich Eckhardt wrote:
>   try:
>       # redirect input() to raw_input() like Python 3
>       input = raw_input
>   except NameError:
>       # no raw input, probably running Python 3 already
>       pass
> What do you think? Any better alternatives?

That's the generic solution, see
http://python3porting.com/differences.html#input-and-raw-input .

In my experience, it seems that input's main function is to allow
beginners to learn the language, or to be used in short scripts. For a
serious application, either curses or moving the input to the invocation
arguments is often a better choice.

- Philipp

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web