python3.8 version has been updated, data analysts and data developers Sight

python3.8 version has been updated, data analysts and data developers Sight

The new version has been released python3.8

As data developers with a switch to give up java python many years, been a long time not been updated python, and also for a long time without some new new features concern the python, and last year his own to do a few small projects updated to python3.6, then this has not put energy into a piece.

Today, because of failure to install a third-party libraries, we intend to look Quguan network updates python found has been updated to python3.8.2, and then act decisively to download and install, the way to learn about new features python3.8 it.

Installation python3.8.2

  • Installation of windows is very simple, direct download msi installation file, double-click on it and then a little bit configuration environment variable.
  • Linux is a bit more complicated, mainly because no suitable yum installation package, the following steps:
# 下载源码
wget https://www.python.org/ftp/python/3.8.2/Python-3.8.2.tgz
# 解压
tar zxf Python-3.8.2.tgz Python-3.8.2/
# 进入文件夹
cd Python-3.8.2/
# 编译前准备
yum update -y
yum groupinstall -y 'Development Tools'
yum install -y gcc openssl-devel bzip2-devel libffi-devel
yum install -y zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel libffi-devel gcc make

# 编译安装
./configure prefix=/usr/local/python3.8 --enable-optimizations
make && make install
# 添加环境变量
export PATH=$PATH:/usr/local/python3.8/bin/

The main new features

Operator Assignment expressions walrus

Python 3.8.0 has a new syntax: =, it will assign a value to a larger variables in the expression. It is kindly referred ** "Walrus operator" (walrus operator) **, because it looks like eyes and walrus ivory.

Before the school has been very self-GOLANG, in this sense python GOLANG want to learn, just as in JAVA learning python.

Now let's look at the following code snippet:

a = range(15)
if (len(a)) > 10:
    print(f"List is too long ({len(a)} elements, expected <= 10)")

In this snippet, we will call len () function twice. Is there any way to avoid re-call to improve readability it? Yes, after improving the code, we get the following results:

a = range(15)
if (n := len(a)) > 10:
	print(f"List is too long ({n} elements, expected <= 10)")

Please try to use the operator walrus clear limits in the case in order to reduce complexity and improve readability.

Positional-only parameters only position parameter

Added a function parameter syntax /used to indicate some form of location function parameter must be used only instead of keyword arguments. This mark syntax by help()using the same tool Argument Clinic of Larry Hastings displayed marked C function. .

# 在下面的例子中,形参 a 和 b 为仅限位置形参,c 或 d 可以是位置形参或关键字形参,而 e 或 f 要求为关键字形参:
def f(a, b, /, c, d, *, e, f):
    print(a, b, c, d, e, f)
   
# 合法的
f(10, 20, 30, d=40, e=50, f=60)

# 不合法的
f(10, b=20, c=30, d=40, e=50, f=60)  # b cannot be a keyword argument
f(10, 20, 30, 40, 50, f=60)  # e must be a keyword argument

With a labeled form of this embodiment is that it allows the full function of a pure Python simulate the behavior of existing functions written in C code. For example, the built-in divmod () function does not accept keyword arguments:

def divmod(a, b, /):
    "Emulate the built in divmod() function"
    return (a // b, a % b)

Python initial configuration (modified embedding)

PYTHONPYCACHEPREFIX new set (or use -X pycache_prefix) may be an implicit byte code cache is configured to use a separate parallel file system tree, instead of the default source directory at each pycache subdirectory.

Location of the cache will be reported in the sys.pycache_prefix (None indicates the default location that is pycache subdirectory).

f - = String Support for automatic recording expressions and commissioning documentation

= Specifier for increasing the f-string. The form f '{expr =}' of f - expressed as Expression extended string of text, plus an equal number, together with evaluation result of the expression. E.g:

>>> user = 'eric_idle'
>>> member_since = date(1975, 7, 31)
>>> f'{user=} {member_since=}'
"user='eric_idle' member_since=datetime.date(1975, 7, 31)"

A general fstring format specifier allows more detailed control expression result to be displayed:

>>> delta = date.today() - member_since
>>> f'{user=!s}  {delta.days=:,d}'
'user=eric_idle  delta.days=16,075'

= The output specifier entire expression so that detailed demonstration of calculation:

>>> print(f'{theta=}  {cos(radians(theta))=:.3f}')
theta=30  cos(radians(theta))=0.866

Other new features

  1. Adds support for regular expressions \ N {name} professional vocabulary
>>> notice = 'Copyright © 2019'
>>> copyright_year_pattern = re.compile(r'\N{copyright sign}\s(\d{4})')
>>> int(copyright_year_pattern.search(notice).group(1))
  1. Dict and dictviews are now iterable in reversed insertion order using reversed()

  2. The remaining properties are more remote, and will not describe, interested students can refer directly to the official document, python3.8 updates .

This article from the blog a mass text multiple tools such as operational platform OpenWrite release

Published 34 original articles · won praise 13 · views 50000 +

Guess you like

Origin blog.csdn.net/frone/article/details/105377285