Python2.6发布,枚举一下我比较感兴趣的新特性

[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]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值