Python 3.7 Adds Data Handling Classes

Python 3.7 Adds Data Handling Classes

The first of four beta previews of Python 3.7 is now available, with a new DataClasses module, an easier way to enter the debugger, and a UTF-8 mode that uses UTF-8 encoding by default.

The scheduled date for the general release of Python 3.7 is June 2018, and this beta is feature complete for that final version.

The first change to Python is the addition of the dataclasses module. This is designed to provide an alternative to the way Python developers had, until now, to use boilerplate code to instantiate classes with a lot of initializers. The new module provides a class decorator that inspects the class’s variable annotations, and based on what is present, adds methods including init, repre and eq to the lass. It is similar to typing.NamedTuple, but also works on classes with mutable instances, among other features.

Another improvement in the 3.7 is a new C API for thread-local storage in CPython. There was already a C API for thread-local storage support; but the existing Thread Local Storage (TLS) API has used int to represent TLS keys across all platforms. This worked fine on officially-support platforms, but meant the code wasn’t particularly portable.The new thread specific storage (TSS) uses a new type Py_tss_t instead of int to represent TSS keys. This is an opaque type, and its definition can be made specific to the underlying TLS implementation, so that it will be possible to build CPython on platforms where the native TLS key is defined in a way that cannot be safely cast to int.

There’s also a new development mode command line option and Pythondevmode environment variable to enable CPython’s “development mode”. This introduces additional runtime checks that are too expensive to be enabled by default. The options that are activated by using the -x dev mode include a debug mode for the asynchronous operations module that offers more detailed logging and exception handling for asynchronous operations; and debug hooks for memory allocators.

Another improvement is a new UTF-8 mode that makes Python ignore the locale and use UTF-8 encoding. Python has supported UTF-8 handling of strings for many years, but if the locale is sett to ASCII, this would until now override the UTF-8.

There are a number of other improvements and some performance improvements including faster method calls, more efficient optimization of source code constants, and faster matching in regular expressions where the match is case insensitive.

More Information

What’s New In Python 3.7

Credit: i-Programmer

  • 1
    Share