Python came out as a result of boredom of its creator Guido van Rossum during the week of Xmas 1989. And now it is powering the most popular websites on internet including YouTube, Reddit, Dropbox, and used by millions of developers worldwide. .
Today, we are going to introduce some of the major changes and added features that you will see in Python 3.7. Let’s begin with the important one –
1. Coercing Legacy C Locale To UTF-8 Based Locale
Till date, determining a feasible default strategy to handle ‘7-bit ASCII’ text encoding assumption implied by the default C locale on platform other than windows, has been an ongoing challenge.
In Python 3.7, the default interpreter command line interface has been upgraded to automatically coerce that locale to a UTF-8 based locale. The automatic setting LC_CTYPE means that both the locale-aware C extensions (like readline) and the core interpreter will assume UTF-8 usage as the default text encoding, instead of ASCII.
Now the default error handler for stdout and stdin is surrogateescape (instead of strict). For stderr, it is still backslashreplace, regardless of locale.
By default, locale coercion is silent. However, to assist in debugging issues associated with locale integration, explicit warnings could be requested by adjusting PYTHONCOERCECLOCALE=warn. It would also enable the Python runtime to return a warning if legacy C locale remains active while initialization of core interpreter.
2. New C API for Thread-Local Storage in CPython
The current TLS (thread local storage) API uses int to represent TLS keys on each platform. But, it’s not POSIX compliant, and also not portable.
The new Python version changes this by offering a new TSS (thread specific storage) API to CPython that overrides the current TLS API within CPython interpreter.
Rather than using int, the new API uses a new type Py_tss_t for representing TSS keys. It would enable you to develop CPython on platforms where native TLS key is defined in a manner that can’t be properly cast to int. All functions of the TLS API would be no-op and immediately return failure.
3. New Built-In Function Breakpoint()
Python 3.7 comes with a new built-in function breakpoint() that enters a Python debugger at the point of the call. It calls sys.breakpointhook() and imports pdb, and then calls pdb.set_trace(). By binding sys.breakpointhook() to the function you are selecting, breakpoint() could enter any debugger.
The new environment variable PYTHONBREAKPOINT() allows external processes to control how breakpoints are handled. Setting PYTHONBREAKPOINT=0 disables debugging, and makes sys.breakpointhook() return None immediately.
4. HTTP Server
HTTP If-Modified-Since header is supported by SimpleHTTPRequestHandler. If you don’t modify the target file after the time specified in the header, the server would return 304 response status.
By default the server serves the current working directory, but you can force the server to use a specific directory by adding –directory to the command line of module server and parameter directory to SimpleHTTPRequestHandler.
5. The Improved Module – argparse
The parse_intermixed_args()and parse_known_intermixed_args() allow you to intermix optional arguments with positional arguments, just like many Unix commands do.
However, these parsers don’t support all the argparse functionalities, and would raise exceptions if unsupported features are used. Specifically, argparse.REMAINDER and mutually exclusive groups, which contain both positional and optional, are not supported.
6. Calendar and CGI
The HTMLCalendar class has new attributes that make it easy to customize CSS classes in the produced HTML calendar.
parse_multipart() returns same outputs as FieldStorage. For non-file fields, the value is list of strings, instead of bytes. It’s easy to use, but not efficient if you are expecting to upload in megabytes. For that, FieldStorage class is quite more flexible.
7. ElementPath and Zipapp
The ElementPath predicates in find()can compare current node’s text via [. = “text”]. It picks all elements whose complete text content (including descendants) equals the given text. Moreover, predicates allow spaces for better readability.
zipapp.create_archive() function takes a filter argument (optional) that enables you to select the files you want to include in the archive, and a compressed argument (optional) to create a compressed archive.
8. Speedup Method Calls and String Search
Python 3.7 comes with two new opcodes – CALL_METHOD and LOAD_METHOD for avoiding instantiation of bound method objects for method calls. This makes method calls faster up to 20 percent as compared to the previous version.
Finding strange Unicode characters (for example “Є”) in a string was previously approximately 25 times slower than searching other usual characters. In Python 3.7, it’s only 3 times slower in worst case.
Also, there has been some improvements made in case-insensitive searching of regular expressions. Now some patterns could be searched up to 20 times faster.
9. C API
The result type in PyThread_get_thread_ident() and PyThread_start_new_thread(), and id parameter of PyThreadState_SetAsyncExc() upgraded from long to unsigned long.
The PyUnicode_AsWideCharString() returns a ValueError if wchar_t* string has null characters and the second argument (size) is NULL.
10. Dis and header for set_trace()
You can use dis() to disassemble nested code objects, which include code of comprehensions, nested functions, generator expression and code used for developing nested classes.
Also, you can now include a header keyword-only argument (optional) in set_trace(). It is printed to the console just before the debugging starts.
Read: 25 Useful Python Frameworks for Developers
Other Minor Improvements
- bytearray.fromhex() and bytes.fromhex() now ignore not only space, but also ASCII whitespace.
- You can can now pass more than 255 arguments to functions, and a function could have more than 255 parameters.
- ImportError shows module name and file path when from … import … crashes.
- b2a_uu() accepts optional ‘backtick’ keyword argument. When it is true, zeros are represented by ‘`’ , rather than spaces.
Leave a reply