聚會時間公告: Kalug 8月份休會一次

[HOT!] 2008 COSCUP 開源人年會2008 8月23日(六)-8月24日(日)熱鬧舉行

六月 19, 2007
» [link] Py3K status.

Python 3000 Status Update 來自BDFL (GvR) 的消息, 說明目前python 3.0的發展狀況.

python 3.0的第一個alpha測試版本將延至今年八月底釋出. (還有兩個月!)
所以預定3.0的最終釋出版本也順延至2008年第二季後.

恩, 照這個估算2009才是python3.0正式的開始.
還有一年半的時間可以假裝這世界上沒有python 3.0 ;)

六月 14, 2007
» PEP 3104 Access to Names in Outer Scopes -- or the ture closures for py3k

http://www.python.org/dev/peps/pep-3104/

python 2.1以前, python是沿用C對scope的思維,
只有很簡單的global跟local的scope概念.
(and builtin)

python 2.1之後引進nested scoping,的確帶來了不少彈性,
不過比起一些更為彈性的語言來說, 對於scoping的控制
仍然無法稱作完善.
而PEP 3104引進了nonlocal這個關鍵字後,
總算讓在python使用closure顯得自然.

目前2.X的狀況:

 
Python 2.4.4 (#2, Apr 5 2007, 20:11:18)
[GCC 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> def x():
... t=9
... def y():
... print t
... t = 10
... print t
... return y
...
>>> z = x()
>>> z()
Traceback (most recent call last):
File "", line 1, in ?
File "", line 4, in y
UnboundLocalError: local variable 't' referenced before assignment


雖然 我們的用意是要讓t被enclosed, 但是卻沒辦法修改t.
注意到為什麼了吗?
由於在scope範圍內有了對t的assignment,
因此python會implicit的認為t是個local變數..
啊 可是我的第一個print t敘述裡的t要的不是被rebind過的t啊~~!

ㄟ.... 老師沒告訴你吗?
"Explicit is better than implicit." :D
所以py3k引入了nonlocal這個keyword,
以往只能用各種hack來解決的這個問題,
(最常用的一種是傳入list[0]),
會相對的使得closure的運用顯得不夠直覺.
在py3k底下則可以寫成這樣:
(現在print 改成 function ,,要括號,, 真容易打錯 :P )


Python 3.0x (p3yk:55958, Jun 13 2007, 18:39:18)
[GCC 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> def x():
... t=9
... def y():
... nonlocal t
... print(t)
... t=10
... print(t)
... return y
...
>>> z = x()
>>> z()
9
10


加入了nonlocal這個declare之後,
是不是比以前清楚的多呢 :)