python2/3的差异

2020-05-29  本文已影响0人  你的学生_张老师

python3的改进

print 成为函数

>>> print('a', 'b', sep='|')
a|b
>>> print('hello', end='$\n')
hello$

python3 不再有Unicode对象,默认str就是Unicode

python2 分为 str, unicode, bytes
python3 只有 str, bytes

python3 除号返回浮点数

python2
>>> 7/2
3
python3
>>> 7/2
3.5
>>> 7//2 
3

python3 增加async协程编程

类型注解

def add(x: int, y: int) -> int:
    return x + y

优化super()

class Base(object):
    def test(self):
        print('test')

python2
class Py2(Base):
    def test(self):
        return super(P2, self).hello()

py2 = Py2()
py2.test()
输出结果:
test

python3
class Py3(Base):
    def test(self):
      return super().hello()

py3 = Py3()
py3.test()
输出结果:
test

高级解包操作

>>> a, *b = [1, 2, 3, 4]
>>> a
1
>>> b
[2, 3, 4]
上一篇 下一篇

猜你喜欢

热点阅读