consistent input() for Python 2 and 3

Ulrich Eckhardt ulrich.eckhardt at dominolaser.com
Thu Aug 2 05:49:27 EDT 2012


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



More information about the Python-list mailing list