Py3k

download Py3k

If you can't read please download the document

Transcript of Py3k

Python 3

@[email protected]://asvetlov.blogspot.com

Releases

2.6 Oct 2008

3.0 Dec 2008

3.1 Jul 2009

2.7 Jul 2010

3.2 Feb 2011

3.3 Aug 2012

unicode

Utf-8

strbytes .

Unified int

Long

Decimal C

"{a:s} {b:.2f}".format(a='', b=3.14159)

"{c[0]}-{c[1]}".format(c=(1, 2))

"{0.x}, {0.y}".format(coord)

Function annotations

def f(a: int, b: float) -> str: return "{}:{}".format(a, b)

>>> f.__annotations__ {'a': builtins.int, 'b': builtins.float, 'return': builtins.str}

Nonlocal

def f(): a = 0 def g(): nonlocal a a += 1 g() return a

Keyword only

>>> def f(a, b=0, *, c, d='Smith'):... pass>>> f(1, c='John', d='Doe')>>> f(1, c='Mary')>>> f(1)Traceback (most recent call last): File "", line 1, in TypeError: f() needs keyword-only argument c

Extended iterable unpacking

a, *rest = range(5) a, *rest, b = range(5)

Comprehensions

{key(k): value(k) for k in range(5)}

{f(k) for k in range(5)}

{1, 2, 3, 4, 5}

ABC and new `super()`

class Base(metaclass=abc.ABCMeta): @abc.abstractmethod def f(self, a): """Comprehensive doc""" pass

class A(Base): def f(self, a): super().f(a)

Exception chain

def f(): try: 1 / 0 except Exception as ex: raise RuntimeError("Oops") from ex f()

Exception chain 2

Traceback (most recent call last): File "", line 3, in f 1/0 ZeroDivisionError: division by zero

The above exception was the direct cause of the following exception:

Traceback (most recent call last): File "", line 7, in f() File "", line 5, in f raise RuntimeError("Division by zero") from ex RuntimeError: Division by zero

Hashable

Iterable

Sized

Container

Callable

Sequence

etc.

OrderedDict

>>> d = {'banana': 3, 'apple':4, 'pear': 1, 'orange': 2}

# dictionary sorted by key>>> OrderedDict(sorted(d.items(), key=lambda t: t[0]))OrderedDict([('apple', 4), ('banana', 3), ('orange', 2), ('pear', 1)])

# dictionary sorted by value>>> OrderedDict(sorted(d.items(), key=lambda t: t[1]))OrderedDict([('pear', 1), ('orange', 2), ('banana', 3), ('apple', 4)])

Counter

>>> cnt = Counter()>>> for word in ['red', 'blue', 'red', 'green', 'blue', 'blue']:... cnt[word] += 1>>> cntCounter({'blue': 3, 'red': 2, 'green': 1})

Yield from

>>> def g(x):... yield from range(x, 0, -1)... yield from range(x)...>>> list(g(5))[5, 4, 3, 2, 1, 0, 1, 2, 3, 4]

class OrderedClass(type): @classmethod def __prepare__(metacls, name, bases, **kwds): return collections.OrderedDict() def __new__(cls, name, bases, classdict): result = type.__new__(cls, name, bases, dict(classdict)) result.members = tuple(classdict) return resultclass A(metaclass=OrderedClass): def one(self): pass def two(self): pass def three(self): pass>>> A.members('__module__', 'one', 'two', 'three')

GILImportlibStable ABIPYC repository dirsABI version tagged .so filesPackaging

?

@[email protected]://asvetlov.blogspot.com