[size=large]Python2.6正式发布
http://www.python.org/
枚举一下我比较感兴趣的新特性
1.
http://docs.python.org/dev/whatsnew/2.6.html#pep-343-the-with-statement
with特性正式启用,文档中这中用法很cool
db_connection = DatabaseConnection()
with db_connection as cursor:
cursor.execute('insert into ...')
cursor.execute('delete from ...')
# ... more operations ...
应该也可以这样
with xxx.profile() as p:
p.xxx=111
p.update_xxx(111)
#结束时刷新缓存
当然,怎么自己写支持with的模块呢?
看
http://docs.python.org/dev/whatsnew/2.6.html#the-contextlib-module
2.
http://docs.python.org/dev/whatsnew/2.6.html#pep-3119-abstract-base-classes
加入了虚函数
from abc import ABCMeta, abstractmethod
class Drawable():
__metaclass__ = ABCMeta
@abstractmethod
def draw(self, x, y, scale=1.0):
pass
def draw_doubled(self, x, y):
self.draw(x, y, scale=2.0)
3.
新的8进制和2进制的表示方式,我喜欢2进制
Python 2.6 doesn’t drop support for a leading 0 signalling an octal number, but it does add support for “0o” and “0b”:
>>> 0o21, 2*8 + 1
(17, 17)
>>> 0b101111
47
4.
Class Decorators
恩,可以少写一点元类了
class A:
pass
A = foo(bar(A))
@foo
@bar
class A:
pass
5.
Per-user site-packages Directory
官方的virtual python方式
6.
很方便的并行技术,有点复杂,只是大概看了一下...
http://docs.python.org/dev/whatsnew/2.6.html#pep-371-the-multiprocessing-package
7.类似moko模板的string构建
http://docs.python.org/dev/whatsnew/2.6.html#pep-3101-advanced-string-formatting
8.
让人不爽的改动
http://docs.python.org/dev/whatsnew/2.6.html#pep-3110-exception-handling-changes
try:
...
except (TypeError, ValueError):#不能写except TypeError, ValueError
...
9.
哇,可以这样.官方也玩magic,我以后有借口了:)
class C(object):
@property
def x(self):
return self._x
@x.setter
def x(self, value):
self._x = value
@x.deleter
def x(self):
del self._x
[/size]
http://www.python.org/
枚举一下我比较感兴趣的新特性
1.
http://docs.python.org/dev/whatsnew/2.6.html#pep-343-the-with-statement
with特性正式启用,文档中这中用法很cool
db_connection = DatabaseConnection()
with db_connection as cursor:
cursor.execute('insert into ...')
cursor.execute('delete from ...')
# ... more operations ...
应该也可以这样
with xxx.profile() as p:
p.xxx=111
p.update_xxx(111)
#结束时刷新缓存
当然,怎么自己写支持with的模块呢?
看
http://docs.python.org/dev/whatsnew/2.6.html#the-contextlib-module
2.
http://docs.python.org/dev/whatsnew/2.6.html#pep-3119-abstract-base-classes
加入了虚函数
from abc import ABCMeta, abstractmethod
class Drawable():
__metaclass__ = ABCMeta
@abstractmethod
def draw(self, x, y, scale=1.0):
pass
def draw_doubled(self, x, y):
self.draw(x, y, scale=2.0)
3.
新的8进制和2进制的表示方式,我喜欢2进制
Python 2.6 doesn’t drop support for a leading 0 signalling an octal number, but it does add support for “0o” and “0b”:
>>> 0o21, 2*8 + 1
(17, 17)
>>> 0b101111
47
4.
Class Decorators
恩,可以少写一点元类了
class A:
pass
A = foo(bar(A))
@foo
@bar
class A:
pass
5.
Per-user site-packages Directory
官方的virtual python方式
6.
很方便的并行技术,有点复杂,只是大概看了一下...
http://docs.python.org/dev/whatsnew/2.6.html#pep-371-the-multiprocessing-package
7.类似moko模板的string构建
http://docs.python.org/dev/whatsnew/2.6.html#pep-3101-advanced-string-formatting
8.
让人不爽的改动
http://docs.python.org/dev/whatsnew/2.6.html#pep-3110-exception-handling-changes
try:
...
except (TypeError, ValueError):#不能写except TypeError, ValueError
...
9.
哇,可以这样.官方也玩magic,我以后有借口了:)
class C(object):
@property
def x(self):
return self._x
@x.setter
def x(self, value):
self._x = value
@x.deleter
def x(self):
del self._x
[/size]