Home > Backend Development > Python Tutorial > Detailed explanation of the new features of the official version of Python 3.6

Detailed explanation of the new features of the official version of Python 3.6

高洛峰
Release: 2017-03-24 17:51:15
Original
1689 people have browsed it

According to the plan on the Python official website, the official version of Python 3.6 is expected to be released on December 16, 2016, which is this Friday. Since May last year, Python 3.6 has been under development, and 4 Alpha versions, 4 Beta versions, and a Candidate version have been released intermittently.
New syntax features
1. Formatted string literals
That is, adding f or F prefix before an ordinary string, the effect is similar to str.format( ). For example,

name = "Fred"
print(f"He said his name is {name}.") # 'He said his name is Fred.'
Copy after login


has the same effect as:

print("He said his name is {name}.".format(**locals()))
Copy after login


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

width = 10
precision = 4
value = decimal.Decimal("12.34567")
print(f"result: {value:{width}.{precision}}") #'result:   12.35'
Copy after login


2, Variable declaration syntax (variable annotations)
is the Typehints that have been available since Python 3.5. In Python3.5, it is used like this:

from typing import List
def test(a: List[int], b: int) -> int:
  return a[0] + b
print(test([3, 1], 2))
Copy after login


The syntax check here is only generated in the editor (such as Pycharm). In actual use, strict checking is not performed.
In Python3.6, a new syntax was introduced:

from typing import List, Dict
primes: List[int] = []
captain: str  # 此时没有初始值
class Starship:
  stats: Dict[str, int] = {}
Copy after login


3. Underscores in Numeric Literals
That is, underscores are allowed to be used in numbers to improve Readability of multi-digit numbers.

a = 1_000_000_000_000_000    # 1000000000000000
b = 0x_FF_FF_FF_FF       # 4294967295
Copy after login


In addition, "String Formatting" also supports the "_" option to print out a more readable numeric string:

'{:_}'.format(1000000)     # '1_000_000'
'{:_x}'.format(0xFFFFFFFF)   # 'ffff_ffff'
Copy after login


4, Asynchronous Generators
In Python3.5, new syntax async and await are introduced to implement coroutines. However, there is a restriction. Yield and await cannot be used simultaneously in the same function body. In Python3.6, this restriction is relaxed. Python3.6 allows the definition of asynchronous generators:

async def ticker(delay, to):
"""Yield numbers from 0 to *to* every *delay* seconds."""
  for i in range(to):
    yield i
    await asyncio.sleep(delay)
Copy after login


5. Asynchronous Comprehensions
allows the use of async for or await syntax in list, set and dictionary parsers.

result = [i async for i in aiter() if i % 2]
result = [await fun() for fun in funcs if await condition()]
Copy after login


Newly added module
A new module has been added to the Python Standard Library (The Standard Library): secrets. This module is used to generate some more secure random numbers to manage data, such as passwords, account authentication, security tokens, and related secrets. For specific usage, please refer to the official documentation: secrets
Other new features
1. The new PYTHONMALLOC environment variable allows developers to set the memory allocator, register debug hooks, etc.
2. The asyncio module is more stable and efficient, and is no longer a temporary module, and the APIs in it are also stable versions.
3. The typing module has also been improved to some extent and is no longer a temporary module.
4. datetime.strftime and date.strftime begin to support ISO 8601 time identifiers %G, %u, %V.
5. The hashlib and ssl modules begin to support OpenSSL1.1.0.
6. The hashlib module begins to support new hash algorithms, such as BLAKE2, SHA-3 and SHAKE.
7. The default encoding of filesystem and console on Windows is changed to UTF-8.
8. The json.load() and json.loads() functions in the json module begin to support binary type input.

The above is the detailed content of Detailed explanation of the new features of the official version of Python 3.6. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template