python2和3的语法区别

发布时间 2023-11-20 14:21:26作者: 腹肌猿
    1. ``  -  python2相当于repr   python3不能使用
    2. Input - python2如果输入字符需要加双引号,数字不需要加  python3 数字字符都可以
    3. Raw_input - python2输入数字字符都可以     python3丢弃
    4. except Exception, e: -  python2可以用   python3 语法报错 ,推荐使用except Exception as e:
    5. range和xrange  - python3已经丢弃xrange,使用range   python2 xrange性能好,返回xrange对象
    6. super  - super(Child, self).__init__() python2和3都可以这样用,但python3也可以写成super().__init__(),python2会报错
    7. print - python2不用加括号,python3必须加
  • class Parent(object):
        def __init__(self):
            print ("this is parent")
    
    class Child(Parent):
        def __init__(self):
            print ("this is CHILD")
            Parent.__init__(self)
            super(Child, self).__init__()
           super().__init__() #python2会报错,3正常
    
    c= Child()