开开心心迁移到 Python3

查看原文

本文介绍了不少 Python 3 的好用的特性。

pathlib 的 / 操作可以避免 os.path.join 的调用:

>>> from pathlib import Path
>>> Path('/') / 'path' / 'to' / 'file'
PosixPath('/path/to/file')
>>> Path('/path/').glob('**/*.jpg') # 使用 glob 递归检测所有目录下的 jpg 图片

Type hinting 可以帮助检测参数类型传错。可以用 enforce 这个库强制在运行时检查类型注解。

可以用 astropy 这个库单位转换:frequency(speed=300_000 * u.km / u.s, wavelength=555 * u.nm)

打印多个值用tab区隔:print(a, b, c sep='\t')