Preview of new features in Python 3.6

  • 2020-05-17 05:53:23
  • OfStack

According to the plan on the Python 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, Python3.6 has been in development, and four versions of Alpha, four versions of Beta and one version of Candidate have been released intermittently.

As a fan of Python, I am looking forward to the release of the new version and hope to try out the new features at the first time. Based on the Python official website article What's New In Python 3.6, this article briefly introduces some new features in Python 3.6.

If you want to try Python 3.6 without destroying the Python environment on your device, Docker is recommended. If not use Docker, may have a look here. / / www ofstack. com article / 94198. htm

New syntax features

1. Formatted string (Formatted string literals)

That is, prefixes f or F to normal strings, which is similar to str.format (). Such as


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 = 10
precision = 4
value = decimal.Decimal("12.34567")
print(f"result: {value:{width}.{precision}}") #'result:   12.35'

2. Variable declaration syntax (variable annotations)

That is, Typehints has existed since Python 3.5. In Python 3.5, it is used as follows:


from typing import List

def test(a: List[int], b: int) -> int:
  return a[0] + b

print(test([3, 1], 2))

The syntax checks here are only generated in the editor (such as Pycharm) and are not strictly checked in actual use.

In Python3.6, a new syntax is introduced:


from typing import List, Dict

primes: List[int] = []
captain: str  #  There is no initial value at this point 

class Starship:
  stats: Dict[str, int] = {}

3. Underline the Numbers (Underscores in Numeric Literals)

This allows the use of underscores in Numbers to improve the readability of multi-digit Numbers.


a = 1_000_000_000_000_000    # 1000000000000000
b = 0x_FF_FF_FF_FF       # 4294967295

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'

4. Asynchronous generator (Asynchronous Generators)

In Python3.5, new syntaxes async and await are introduced to implement the co-program. However, there is a limitation that you cannot use both yield and await in the same function. This restriction is relaxed in Python3.6, which allows you to define 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)

5. Asynchronous parser (Asynchronous Comprehensions)

This allows the use of async for or await grammars in the list list, set collection, and dict dictionary parsers.


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

New module added

A new module, secrets, has been added to the Python standard library (The Standard Library). This module is used to generate some more secure random Numbers for data management, such as passwords, account authentication, security tokens, related secrets, account authentication, security tokens, and related secrets. The specific usage can refer to the official document: secrets

Other new features

1. The new PYTHONMALLOC environment variable allows developers to set up memory allocators and register debug hooks.

2. The asyncio module is more stable and efficient, and it is no longer a temporary module, among which API is also a stable version.

3. The typing module has also been improved, and it is no longer a temporary module.

4. datetime.strftime and date.strftime begin to support the time identifiers %G, %u, %V of ISO 8601.

5. 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 input of type binary.

9,...

There are many other features, but that's about as many as you can use in your day job. Interested readers can refer directly to the official documentation: What' ess New In Python 3.6


Related articles: