Detailed Python3.6 The new features of the official edition

Source: Internet
Author: User
According to the Python official online plan, the official version of Python3.6 is expected to be released in 2016-12-16, the Friday. Since last May, the Python3.6 version has been developed, with 4 alpha, 4 beta, and one candidate released intermittently.
New syntax features
1. Formatted string (formatted string literals)
The f or F prefix is added before the normal string, and the effect is similar to Str.format (). Like what

Name = "Fred" Print (f "He said his name is {name}.") # ' He said his name is Fred. '


The effect is equivalent to:

Print ("He said his name is {name}.". Format (**locals ()))


In addition, this feature supports nested fields, such as:

width = 10precision = 4value = Decimal. Decimal ("12.34567") print (f "Result: {value:{width}.{ Precision}} ") # ' Result:   12.35 '


2. Variable declaration syntax (variable annotations)
That is, from the beginning of Python3.5 typehints. In Python3.5, it is used in the following way:

From typing import listdef test (A:list[int], b:int), int:  return a[0] + bprint (Test ([3, 1], 2))


The syntax check here is only generated in the editor (such as Pycharm) and is not strictly checked in actual use.
In Python3.6, a new syntax is introduced:

From typing import List, dictprimes:list[int] = []captain:str  # There is no initial value at this time class starship:  stats:dict[str, int] = { }


3, the number of the underline notation (underscores in Numeric literals)
This allows the use of underscores in numbers to improve the readability of multiple digits.

A = 1_000_000_000_000_000    # 1000000000000000b = 0x_ff_ff_ff_ff       # 4294967295


In addition, "string formatting" also supports the "_" option to print a more readable string of numbers:

' {: _} '. Format (1000000)     # ' 1_000_000 ' {: _x} '. Format (0xFFFFFFFF)   # ' FFFF_FFFF '


4. Asynchronous generator (asynchronous generators)
In Python3.5, new syntax async and await are introduced to implement the cooperative program. However, there is a limitation that you cannot use yield and await simultaneously in the same function body, and in Python3.6, this restriction is released, and the asynchronous generator is allowed in Python3.6:

Async DEF Ticker (delay, to): "" "Yield numbers from 0 to *to* every *delay* seconds.  " " For I in range:    yield i    await asyncio.sleep (delay)


5. Asynchronous resolver (asynchronous comprehensions)
This allows the use of the async for or await syntax in list, set, and dictionary dict parsers.

result = [I async for I in Aiter () if I percent 2]result = [await fun () ' for fun in Funcs if await condition ()]


New Add-on module
A new module has been added to the standard library of Python: Secrets. The module is used to generate some more secure random numbers for managing data such as passwords, account authentication, security tokens, and related secrets. For specific usage, refer to the official documentation: Secrets
Other new features
1. The new PYTHONMALLOC environment variable allows the developer to set the memory allocator and register the debug hooks.
2, Asyncio module is more stable, efficient, and is no longer a temporary module, where the API is also stable version of the.
3, typing module also has a certain improvement, and is no longer a temporary module.
4, Datetime.strftime and date.strftime start supporting ISO 8601 time identifiers%g,%u,%V.
5, Hashlib and SSL modules start to support OpenSSL1.1.0.
6, Hashlib module began to support new hash algorithms, such as BLAKE2, SHA-3 and SHAKE.
7. The FileSystem and console default encoding on Windows changes to UTF-8.
8. The Json.load () and json.loads () functions in the JSON module begin to support binary type input.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.