zoukankan      html  css  js  c++  java
  • python 3.x与python 2.7.x在语法上的区别

    (1)去除了<>,全部改用!= 
    (2)去除``,全部改用repr() 
    (3)关键词加入as 和with,还有True,False,None 
    (4)整型除法返回浮点数,要得到整型结果,请使用// 
    (5)加入nonlocal语句。使用noclocal x可以直接指派外围(非全局)变量 
    (6)去除print语句,加入print()函数实现相同的功能。同样的还有 exec语句,已经改为exec()函数 
       例如: 
         2.X: print "The answer is", 2*2 
         3.X: print("The answer is", 2*2) 
         2.X: print x,                              # 使用逗号结尾禁止换行 
         3.X: print(x, end=" ")                     # 使用空格代替换行 
         2.X: print                                 # 输出新行 
         3.X: print()                               # 输出新行 
         2.X: print >>sys.stderr, "fatal error" 
         3.X: print("fatal error", file=sys.stderr) 
         2.X: print (x, y)                          # 输出repr((x, y)) 
         3.X: print((x, y))                         # 不同于print(x, y)! 
    (7)改变了顺序操作符的行为,例如x<y,当x和y类型不匹配时抛出TypeError而不是返回随即的 bool值  
    (8)输入函数改变了,删除了raw_input,用input代替: 
       2.X:guess = int(raw_input('Enter an integer : ')) # 读取键盘输入的方法 
       3.X:guess = int(input('Enter an integer : '))
    (9)去除元组参数解包。不能def(a, (b, c)):pass这样定义函数了 
    (10)新式的8进制字变量,相应地修改了oct()函数。 
       2.X的方式如下: 
         >>> 0666 
         438 
         >>> oct(438) 
         '0666' 
       3.X这样: 
         >>> 0666 
         SyntaxError: invalid token (<pyshell#63>, line 1) 
         >>> 0o666 
         438 
         >>> oct(438) 
         '0o666' 
    (11)增加了 2进制字面量和bin()函数 
        >>> bin(438) 
        '0b110110110' 
        >>> _438 = '0b110110110' 
        >>> _438 
        '0b110110110' 
    (12)扩展的可迭代解包。在Py3.X 里,a, b, *rest = seq和 *rest, a = seq都是合法的,只要求两点:rest是list 
    对象和seq是可迭代的。 
    (13)新的super(),可以不再给super()传参数, 
        >>> class C(object): 
              def __init__(self, a): 
                 print('C', a) 
        >>> class D(C): 
              def __init(self, a): 
                 super().__init__(a) # 无参数调用super() 
        >>> D(8) 
        C 8 
        <__main__.D object at 0x00D7ED90> 
    (14)新的metaclass语法: 
        class Foo(*bases, **kwds): 
          pass 
    (15)支持class decorator。用法与函数decorator一样: 
        >>> def foo(cls_a): 
              def print_func(self): 
                 print('Hello, world!') 
              cls_a.print = print_func 
              return cls_a 
        >>> @foo 
        class C(object): 
          pass 
        >>> C().print() 
        Hello, world! 

    (16) Python引入很多新的特性, python 2.7.x需要继承object类才可以使用, 在python 2.7.x的文档中, 有标注:

    如: Note xxx() only works for new-style classes. 则需要继承object类才可以使用, 否则无效;

    python 3.x中, 则隐式(implicit)继承object类, 即新型式的类(new-style class), 则不需要继承object类

  • 相关阅读:
    计算机网络 学习笔记-传输层:TCP协议简介
    C/C++里的const(2)
    C语言变量声明加冒号的用法
    CTL_CODE说明
    FreeImage.lib库的配置和简单使用 转
    WSASocket()与Socket()的区别 转
    Win7下运行VC程序UAC权限问题 VC2010设置UAC权限方法
    MFC通过URL下载并保存文件代码 转载
    opencv 数据类型转换:CvArr, Mat, CvMat, IplImage, BYTE 转
    ISIS Scanner Errors codes
  • 原文地址:https://www.cnblogs.com/zpf1092841490/p/8135872.html
Copyright © 2011-2022 走看看