Backwards-compatible input calls in Python

后端 未结 4 1743
囚心锁ツ
囚心锁ツ 2020-12-01 18:12

I was wondering if anyone has suggestions for writing a backwards-compatible input() call for retrieving a filepath?

In Python 2.x, raw_input worked fine for input l

4条回答
  •  囚心锁ツ
    2020-12-01 18:26

    Since the Python 2.x version of input() is essentially useless, you can simply overwrite it by raw_input:

    try:
        input = raw_input
    except NameError:
        pass
    

    In general, I would not try to aim at code that works with both, Python 2.x and 3.x, but rather write your code in a way that it works on 2.x and you get a working 3.x version by using the 2to3 script.

提交回复
热议问题