<font color="#000000"><br></font><br><div class="gmail_quote">On Thu, Aug 2, 2012 at 5:49 AM, Ulrich Eckhardt <span dir="ltr"><<a href="mailto:ulrich.eckhardt@dominolaser.com" target="_blank">ulrich.eckhardt@dominolaser.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi!<br>
<br>
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.<br>
<br>
<br>
My approach is to simply do this:<br>
<br>
  try:<br>
      # redirect input() to raw_input() like Python 3<br>
      input = raw_input<br>
  except NameError:<br>
      # no raw input, probably running Python 3 already<br>
      pass<br>
<br>
<br>
What do you think? Any better alternatives?<br></blockquote><div><br></div><div>Depending on how much user input is needed in your application, you can always use the 'cmd' module: <a href="http://docs.python.org/library/cmd.html">http://docs.python.org/library/cmd.html</a></div>
<div><br></div><div>It is present in both Python 2 and Python 3 and should just 'do the right thing'.  It also seamlessly integrates readline (if present), command-completion, and provides a built-in help menu for defined commands.</div>
<div><br></div><div>It's written in pure Python, and in my opinion, the best form of documentation for that module is the source code itself.</div><div><br></div><div>I haven't used it in Python 3, but I imagine it can be used in a way that easily supports Python 2 and 3.  If you have only one or two places where you need user-input, this is probably overkill.</div>
<div><br></div><div>HTH,</div><div>Jason</div></div>