What’s New In Python 3.3 — Python 3.16.0a1 documentation
Navigation index modules | next | previous | Python » 3.16.0a1 Documentation » What’s New in Python » What’s New In Python 3.3 | Theme | What’s New In Python 3.3 ¶ This article explains the new features in Python 3.3, compared to 3.2. Python 3.3 was released on September 29, 2012. For full details, see the changelog . See also PEP 398 - Python 3.3 Release Schedule Summary – Release highlights ¶ New syntax features: New yield from expression for generator delegation .
The u'unicode' syntax is accepted again for str objects. New library modules: faulthandler (helps debugging low-level crashes) ipaddress (high-level objects representing IP addresses and masks) lzma (compress data using the XZ / LZMA algorithm) unittest.mock (replace parts of your system under test with mock objects) venv (Python virtual environments , as in the popular virtualenv package) New built-in features: Reworked I/O exception hierarchy .
Implementation improvements: Rewritten import machinery based on importlib . More compact unicode strings . More compact attribute dictionaries . Significantly Improved Library Modules: C Accelerator for the decimal module. Better unicode handling in the email module ( provisional ). Security improvements: Hash randomization is switched on by default. Please read on for a comprehensive list of user-facing changes. PEP 405: Virtual Environments ¶ Virtual environments help create separate Python setups while sharing a system-wide base install, for ease of maintenance.
Virtual environments have their own set of private site packages (i.e. locally installed libraries), and are optionally segregated from the system-wide site packages. Their concept and implementation are inspired by the popular virtualenv third-party package, but benefit from tighter integration with the interpreter core. This PEP adds the venv module for programmatic access, and the pyvenv script for command-line access and administration.
The Python interpreter checks for a pyvenv.cfg , file whose existence signals the base of a virtual environment’s directory tree. See also PEP 405 - Python Virtual Environments PEP written by Carl Meyer; implementation by Carl Meyer and Vinay Sajip PEP 420: Implicit Namespace Packages ¶ Native support for package directories that don’t require __init__.py marker files and can automatically span multiple path segments (inspired by various third party approaches to namespace packages, as described in PEP 420 ) See also PEP 420 - Implicit Namespace Packages PEP written by Eric V.
Smith; implementation by Eric V. Smith and Barry Warsaw PEP 3118: New memoryview implementation and buffer protocol documentation ¶ The implementation of PEP 3118 has been significantly improved. The new memoryview implementation comprehensively fixes all ownership and lifetime issues of dynamically allocated fields in the Py_buffer struct that led to multiple crash reports. Additionally, several functions that crashed or returned incorrect results for non-contiguous or multi-dimensional input have been fixed.
The memoryview object now has a PEP-3118 compliant getbufferproc() that checks the consumer’s request type. Many new features have been added, most of them work in full generality for non-contiguous arrays and arrays with suboffsets. The documentation has been updated, clearly spelling out responsibilities for both exporters and consumers. Buffer request flags are grouped into basic and compound flags. The memory layout of non-contiguous and multi-dimensional NumPy-style arrays is explained.
Features ¶ All native single character format specifiers in struct module syntax (optionally prefixed with ‘@’) are now supported. With some restrictions, the cast() method allows changing of format and shape of C-contiguous arrays. Multi-dimensional list representations are supported for any array type. Multi-dimensional comparisons are supported for any array type. One-dimensional memoryviews of hashable (read-only) types with formats B, b or c are now hashable. (Contributed by Antoine Pitrou in bpo-13411 .) Arbitrary slicing of any 1-D arrays type is supported.
For example, it is now possible to reverse a memoryview in O (1) by using a negative step. API changes ¶ The maximum number of dimensions is officially limited to 64. The representation of empty shape, strides and suboffsets is now an empty tuple instead of None . Accessing a memoryview element with format ‘B’ (unsigned bytes) now returns an integer (in accordance with the struct module syntax). For returning a bytes object the view must be cast to ‘c’ first. memoryview comparisons now use the logical structure of the operands and compare all array elements by value.
All format strings in struct module syntax are supported. Views with unrecognised format strings are still permitted, but will always compare as unequal, regardless of view contents. For further changes see Build and C API Changes and Porting C code . (Contributed by Stefan Krah in bpo-10181 .) See also PEP 3118 - Revising the Buffer Protocol PEP 393: Flexible String Representation ¶ The Unicode string type is changed to support multiple internal representations, depending on the character with the largest Unicode ordinal (1, 2, or 4 bytes) in the represented string.
This allows a space-efficient representation in common cases, but gives access to full UCS-4 on all systems. For compatibility with existing APIs, several representations may exist in parallel; over time, this compatibility should be phased out. On the Python side, there should be no downside to this change. On the C API side, PEP 393 is fully backward compatible. The legacy API should remain available at least five years.
Applications using the legacy API will not fully benefit of the memory reduction, or - worse - may use a bit more memory, because Python may have to maintain two versions of each string (in the legacy format and in the new efficient storage). Functionality ¶ Changes introduced by PEP 393 are the following: Python now always supports the full range of Unicode code points, including non-BMP ones (i.e. from U+0000 to U+10FFFF ).
The distinction between narrow and wide builds no longer exists and Python now behaves like a wide build, even under Windows. With the death of narrow builds, the problems specific to narrow builds have also been fixed, for example: len() now always returns 1 for non-BMP characters, so len('\U0010FFFF') == 1 ; surrogate pairs are not recombined in string literals, so '\uDBFF\uDFFF' != '\U0010FFFF' ; indexing or slicing non-BMP characters returns the expected value, so '\U0010FFFF'[0] now returns '\U0010FFFF' and not '\uDBFF' ; all other functions in the standard library now correctly handle non-BMP code points.
The value of sys.maxunicode is now always 1114111 ( 0x10FFFF in hexadecimal). The PyUnicode_GetMax() function still returns either 0xFFFF or 0x10FFFF for backward compatibility, and it should not be used with the new Unicode API (see bpo-13054 ). The ./configure flag --with-wide-unicode has been removed. Performance and resource usage ¶ The storage of Unicode strings now depends on the highest code point in the string: pure ASCII and Latin1 strings ( U+0000-U+00FF ) use 1 byte per code point; BMP strings ( U+0000-U+FFFF ) use 2 bytes per code point; non-BMP strings ( U+10000-U+10FFFF ) use 4 bytes per code point.
The net effect is that for most applications, memory usage of string storage should decrease significantly - especially compared to former wide unicode builds - as, in many cases, strings will be pure ASCII even in international contexts (because many strings store non-human language data, such as XML fragments, HTTP headers, JSON-encoded data, etc.). We also hope that it will, for the same reasons, increase CPU cache efficiency on non-trivial applications.
The memory usage of Python 3.3 is two to three times smaller than Python 3.2, and a little bit better than Python 2.7, on a Django benchmark (see
The rest of this page is on the live site.