`
zuroc
  • 浏览: 1295265 次
  • 性别: Icon_minigender_1
  • 来自: 江苏
社区版块
存档分类
最新评论

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

阅读更多
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
分享到:
评论
3 楼 mathgl 2008-10-13  
with 在delphi上也有。。不过我不怎么用。
2 楼 mathgl 2008-10-10  
multiprocess 感觉一般
在*nix下可以用

win32下很多obj不能picklable的,例如com,third-party extension那种
都不能正常使用。
1 楼 javaeyename 2008-10-03  
with,想当年vb的意淫的玩意儿,呵呵!不过倒是蛮方便的!

相关推荐

Global site tag (gtag.js) - Google Analytics