Writing Python 2.7 code that is as close to Python 3.x syntax as possible

前端 未结 7 728
情深已故
情深已故 2020-12-23 02:29

Since Django doesn\'t yet support Python 3.x, I\'m using Python 2.7. However, I\'d like to go ahead and start familiarizing myself with the new Python 3.x syntax as much as

7条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-23 02:59

    You also need to use the new exception syntaxes, ie no more

    try:
         raise Exception, "Message"
    except Exception, e:
         pass
    

    instead you should do:

    try:
         raise Exception("Message")
    except Exception as e:
         pass
    

    Also make sure you prefix all your binary strings with a b, ie:

    b'This is a binary string'

    For a more complete cover of this topic, see http://python3porting.com/noconv.html

提交回复
热议问题