Python's Archiver

為方便港臺同胞閱覽,Python中國特別推出簡繁體內容轉換功能

xieaotian 发表于 2008-10-11 20:55

Python2.6正式发布

Python2.6正式发布[url]http://www.python.org/download/releases/2.6/[/url]
出现了,

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的模块呢?

[url]http://docs.python.org/dev/whats[/url] ... e-contextlib-module


2.
[url]http://docs.python.org/dev/whats[/url] ... -abstract-base-c...
加入了虚函数


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.
很方便的并行技术,有点复杂,只是大概看了一下...


[url]http://docs.python.org/dev/whats[/url] ... the-multiprocess...


7.类似moko模板的string构建


[url]http://docs.python.org/dev/whats[/url] ... -advanced-string...


8.
让人不爽的改动


[url]http://docs.python.org/dev/whats[/url] ... -exception-handl...


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

页: [1]

Powered by Discuz! Archiver 6.1.0  © 2001-2007 Comsenz Inc.