Python3中的新特性(1)——新的语言特性

1.源代码编码和标识符
        Python3假定源代码使用UTF-8编码。另外,关于标识符中哪些字符是合法的规则也放宽了。特别是,标识符可以包含代码点为U+0080及以上的任意有效Unicode字符。例如:
        π = 3.1415926
2.集合字面量
        在{}中放入一组值就可以定义一个集合,例如:
        days = {'Mon','Tue','Wed','Thu','Fri','Sat','Sun'}
同:
        days = set(['Mon','Tue','Wed','Thu','Fri','Sat','Sun'])
3.集合和字典包含
语法{expr for x in s if condition}称为集合包含。它将一种操作应用给集合s的所有元素,然后返回一个集合。
语法{kexpr:vexpr for k,v in s if condition}称为字典包含。它将一种操作应用给(key,value)元组序列s中的所有键和值。然后返回一个字典。
4.扩展的可迭代项解包
在Python2中,使用如下语法将一个可迭代项解包到一些变量中:
items = [1,2,3,4]
a,b,c,d = items        #将各个项解包到变量中
只有变量个数与要解包的项数完全相等,解包才能进行。
在Python3中,可以使用通配符变量仅仅解包一个序列中的某些项,而将其他值放到一个列表中,例如:
a,*rest = items            #a = 1 ,rest = [2,3,4]
a,*rest,d = items         #a = 1 ,rest = [2,3],d = 4 
*rest,d = items            #rest = [1,2,3],d = 4
使用这种语法时,只能出现一个带有*号的变量。
5.nonlocal变量
使用nonlocal声明后,内部函数可以修改外部函数中的变量,例如:
def  countdown(n):
       def decrement():
             nonlocal n
             n -= 1
       while n > 0:
                print('T-minus',n)
                decrement()
Python2中,内部函数可以读取外部函数的变量,但不能修改它们,使用nonlocal声明后就能修改它们了。
6.函数注释
函数与方法在定义时都可以带有注释,其通常语法为:
def functionName(par1:exp1,par2:exp2,...,parN:expN)->rexp:
    suite
每个冒号表达式部分(:expX)是一个可选的注释,箭头返回表达式部分(->rexp)也是。
如果存在注释,就会被添加到__annotations__字典中;如果不存在,那么此字典为空。该字典的键为参数名,值为相应的表达式。这种语法格式允许我们对所有、部分或不对任何参数进行注释,对返回值也是如此。同样的语法也使用于各种位置参数和关键字参数。
def  foo(x:1,y:2) -> 3:
        pass
>>>foo.__annotations__
{'y':2,'x':1,'return':3}
7.只能通过关键字引用的参数
函数可以指定只能通过关键字引用的参数,方法是在第一个带*号的参数后定义额外的参数,例如:
def foo(x,*args,strict = False):
        statements
调用此函数时,只能以关键字的形式指定strict参数,例如:
a = foo(1,strict = True)
如果参数的数量不定,但又要使用只能通过关键字引用的参数,可以在参数列表中使用一个单独的*号,例如:
def foo(x,*,strict = False):
        statements
8.省略号表达式
Ellipsis对象(...)可以用作表达式,从而可放在容器中或赋值给变量,例如:
>>>x = ...        #赋值为省略号
>>>x
Ellipsis
>>>a = [1,2,...]
>>>a
[1,2,Ellipsis]
>>>... in a
True
>>>x is ...
True
9.链接异常
现在可以将异常链接在一起。从本质上讲,这是让当前异常携带与前面异常相关信息的一种途径。from限定符与raise语句在一起使用便可显示链接异常。例如:
try:
        statements
except  ValueError  as  e:
        raise SyntaxError('Could not parse configuration')  from e
引发SyntaxError异常时,就会生成像下面这样一条跟踪信息,同时显示两个异常:
Traceback (most recent call last):
  File "<pyshell#3>", line 1, in <module>
    raise ValueError
ValueError
 
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
  File "<pyshell#4>", line 1, in <module>
    raise SyntaxError('Could not parse configuration')
SyntaxError: Could not parse configuration
异常对象的__cause__属性被置为前一个异常,使用from限定符和raise语句可以设置该属性。
有趣的异常链接:在另一个异常处理器中引发异常,例如:
def error(msg):
        print(m)                    #m未定义
try:
        statements
except ValueError as e:
        error('Could not parse configuration')
如果在Python2中执行这段代码,在error()中只有一个与NameError相关的异常。而在Python3中,前一个要处理的异常与结果链接在了一起:
Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    raise ValueError
ValueError
 
During handling of the above exception, another exeption occurred:
 
Traceback (most recent call last):
  File "<pyshell#1>", line 1, in <module>
    print m
NameError: name 'm' is not defined
10.经过改进的super()函数
super()函数用于查找基类中的方法,在Python3中它可以不带任何参数运行,例如:
class C(A,B):
        def bar(self):
                return super().bar()        #调用基类中的bar()方法
而在Python2中,必须使用super(C,self).bar()。
11.高级元类
在Python3中,元类可以在类主体执行之前完成一些额外的工作,方法是在元类中定义一个特殊的类方法__prepare__(cls,name,bases,**kwargs)。该方法的返回结果必须是一个字典,而类定义的主体执行时将填充这个字典。例如:
class MyMeta(type):
    @classmethod
    def __prepare__(cls,name,bases,**kwargs):
        print('preparing',name,bases,kwargs)
        return {}
    def __new__(cls,name,bases,classdict):
        print('creating',name,bases,classdict)
        return type.__new__(cls,name,bases,classdict)
class Foo(metaclass=MyMeta):    
    print('About to define methods')
    def __init__(self):
        pass
    def bar(self):
        pass
    print('Done defining methods')
运行以上代码后,在输出中会看到控制流的走向:
preparing Foo () {}
About to define methods
Done defining methods
'creating', 'Foo', (), {'bar': <function bar at 0x03845d0>, '__module__': '__main__', '__init__': <function __init__ at 0x384588>}
元类的__prepare__方法上的额外关键字参数是从class语句的基类列表中使用的关键字参数传递而来。例如语句class Foo(metaclass=MyMeta,spam=42,blah='Hello')将关键字参数spam和blah传递给MyMeta.__prepare__()方法。这种约定可以将任意配置信息传递给元类。
 
原文地址:https://www.cnblogs.com/Camilo/p/3332254.html