Differences between revisions 6 and 7
Revision 6 as of 2007-04-10 05:26:01
Size: 3378
Editor: HuangYi
Comment:
Revision 7 as of 2007-04-10 05:28:33
Size: 3452
Editor: HuangYi
Comment:
Deletions are marked like this. Additions are marked like this.
Line 53: Line 53:
新的 class 定义语法和函数几乎一样,除了 def 换成了 class: 新的 class 定义语法和函数调用几乎一样,除了 def 换成了 class:
Line 77: Line 77:
==== Keyword-Only Arguments ====
[wiki:peps:pep-3102/ PEP 3102]

即将到来的 Python 3000 ::-- ["swordsp"] [DateTime] TableOfContents

概述

  • [wiki:peps:pep-3000/ PEP 3000] -- Python 3000
  • [wiki:peps:pep-3099/ PEP 3099] -- Things that will Not Change in Python 3000
  • [wiki:peps:pep-3100/ PEP 3100] -- Miscellaneous Python 3.0 Plans

PEPs

Access to Names in Outer Scopes

[wiki:peps:pep-3104/ PEP 3104]

  • 这算是个老问题了:
       1 a = 1
       2 def test1():
       3     a = 2
       4     def test2():
       5         #目前的python版本中,这里无法修改 test1 中的那个 a。
    
    而 python3000 中有望通过使用 nonlocal 关键字访问外层名字空间:
       1 a = 1
       2 def test1():
       3     a = 2
       4     def test2():
       5         nonlocal a = 3
       6         # 这样就把 test1 中的 a 修改了。
    
    nonlocal 会从最靠近的名字空间不断向外搜索。

Function Annotations

[wiki:peps:pep-3107/ PEP 3107]

  • 可以可选地给函数添加元数据,利用这些元数据可以干很多事情,比如文档生成、类型检查、方便与静态语言之间的交互等等,看个例子:
       1 def foo(a: 'x', b: 5 + 6, c: list) -> max(2, 9):
       2     ...
    

Standard Library Reorganization

[wiki:peps:pep-3108/ PEP 3108]

  • 重新组织标准库,主要是移除和重命名。

Metaclasses in Python 3000

[wiki:peps:pep-3115/ PEP 3115]

Python 3000 在 metaclass 方面的大改动! 新的 class 定义语法和函数调用几乎一样,除了 def 换成了 class:

   1 class Foo(base1, base2, metaclass=mymeta, otherkeyword=...):
   2     ...
   3 
   4 bases = (base1, base2)
   5 keywords = {'metaclass':mymeta, 'a':1, 'b':2}
   6 class Foo(*bases, **kw):
   7     ....

另外以前的 metaclass 第一次被调用时,class 的属性字典就已经构建出来了,metaclass 无法控制这个过程,使得有些功能无法实现,比如保持属性定义的顺序! Python 3000 中的 metaclass 可以定义一个叫 __prepare__ 的 class method,返回一个 dict like 对象(只需要定义 __setitem__ 方法),用来控制 class 属性的存储。这个过程用伪码表示大致如下:

   1        def prepare_class(name, *bases, metaclass=None, **kwargs):
   2           if metaclass is None:
   3              metaclass = compute_default_metaclass(bases)
   4           prepare = getattr(metaclass, '__prepare__', None)
   5           if prepare is not None:
   6              return prepare(name, bases, **kwargs)
   7           else:
   8              return dict()

可以看出,class 定义时传入的参数实际上都是传给 metaclass 的。

Keyword-Only Arguments

[wiki:peps:pep-3102/ PEP 3102]

进展

讨论

PageComment2

Python3000 (last edited 2009-12-25 07:16:15 by localhost)