Why upgrade to Python 3.7
Python 3.7 was released in June 2018, adding performance to common operations, and adds user-visible changes in the following categories.
The boilerplate copy-paste required for Python classes can seem inelegant.
Python 3.7
data class
eliminates the boilerplate code in initializing classes.
The @dataclass
decorator enables this template.
@dataclass
class Rover:
'''Class for robotic rover.'''
name: str
uid: int
battery_charge: float=0.
temperature: float
def check_battery_voltage(self) -> float:
return self.aioread(port35) / 256 * 4.1
Python 3.7 introduced breakpoint, which breaks into the debugger.
x=1
y=0
breakpoint()
z = x/y
It’s very common to have more than one version of Python installed.
Likewise, multiple versions of the same library may be installed, overriding other versions.
For example, system Numpy may be overridden with a pip
installed Numpy.
Python ≥ 3.7 gives the absolute path and filename from which the ImportError
was generated.
from numpy import blah
Python < 3.7:
ImportError: cannot import name ‘blah’
Python ≥ 3.7:
ImportError: cannot import name ‘blah’ from ’numpy’ (c:/Python37/Lib/site-packages/numpy/init.py)
The popular and efficient argparse
module can now handle intermixed positional and optional arguments, just like the shell.
from argparse import ArgumentParser
p = ArgumentParser()
p.add_argument('xmlfn')
p.add_argument('--plottype')
p.add_argument('indices',nargs='*',type=int)
p = p.parse_intermixed_args() # instead of p.parse_args()
print(p)
python myprogram.py my.xml 26 --plottype inv 2 3
Namespace(indices=[26,2,3], plottype=‘inv’, xmlfm=‘my.xml’)
whereas if you have used p.parse_args()
you would have gotten
error: unrecognized arguments: 2 3
Note: optparse
was deprecated in 2011 and is no longer maintained.
Python ≥ 3.7 can do
import a.b as c
instead of Python ≤ 3.6 needing
from a import b as c
The discussion makes the details clear for those who are really interested in Python import behavior.
Python ≥ 3.7 disassembler
dis.dis()
can reach more deeply inside Python code, adding a depth
parameter useful for recursive functions, and elements including:
- list comprehension:
x2 = [x**2 for x in X]
(greedy eval) - generator expressions:
x2 = (x**2 for x in X)
(lazy eval)
Case-insensitive regex sped up by as much as 20x.
Python 3.7 added constants that allow controlling subprocess
priority in Windows.
This allows keeping the main Python program at one
execution priority, while launching subprocesses at another priority.
The ability to start subprocesses without opening a new console window is enabled by
subprocess.CREATE_NO_WINDOW
.
The confusingly named but important universal_newlines
boolean parameter is now named text
.
When text=True
, stdin/stderr/stdout will emit/receive text stream instead of bytes
stream.