Python 3特色用法:新特性汇总
2023-03-07 09:51:53 时间
这篇文章灵感来源于一个新项目 A short guide on features of Python 3 for data scientists,这个项目列出来了作者使用Python 3用到的一些特性。正巧我最近也想写一篇介绍Python 3(特指Python 3.6+)特色用法的文章。开始吧!
pathlib模块
pathlib模块是Python 3新增的模块,让你更方便的处理路径相关的工作。
- In : from pathlib import Path
- In : Path.home()
- Out: PosixPath('/Users/dongweiming') # 用户目录
- In : path = Path('/user')
- In : path / 'local' # 非常直观
- Out: PosixPath('/user/local')
- In : str(path / 'local' / 'bin')
- Out: '/user/local/bin'
- In : f = Path('example.txt')
- In : f.write_bytes('This is the content'.encode('utf-8'))
- Out[16]: 19
- In : with f.open('r', encoding='utf-8') as handle: # open现在是方法了
- ....: print('read from open(): {!r}'.format(handle.read()))
- ....:
- read from open(): 'This is the content'
- In : p = Path('touched')
- In : p.exists() # 集成了多个常用方法
- Out: False
- In : p.touch()
- In : p.exists()
- Out: True
- In : p.with_suffix('.jpg')
- Out: PosixPath('touched.jpg')
- In : p.is_dir()
- Out: False
- In : p.joinpath('a', 'b')
- Out: PosixPath('touched/a/b')
可迭代对象的解包
- In : a, b, *rest = range(10) # 学过lisp就很好懂了,相当于一个「everything else」
- In : a
- Out: 0
- In : b
- Out: 1
- In : rest
- Out: [2, 3, 4, 5, 6, 7, 8, 9]
- In : *prev, next_to_last, last = range(10)
- In : prev, next_to_last, last
- Out: ([0, 1, 2, 3, 4, 5, 6, 7], 8, 9)
强制关键字参数
使用强制关键字参数会比使用位置参数表意更加清晰,程序也更加具有可读性,那么可以让这些参数强制使用关键字参数传递,可以将强制关键字参数放到某个 参数或者单个 后面就能达到这种效果:
- In : def recv(maxsize, *, block):
- ....:
- ....: pass
- ....:
- In : recv(1024, True)
- ---------------------------------------------------------------------------
- TypeError Traceback (most recent call last)
- <ipython-input-49-8e61db2ef94b> in <module>()
- ----> 1 recv(1024, True)
- TypeError: recv() takes 1 positional argument but 2 were given
- In : recv(1024, block=True)
通配符**
我们都知道在Python 2时不能直接通配递归的目录,需要这样:
- found_images = \
- glob.glob('/path/*.jpg') \
- + glob.glob('/path/*/*.jpg') \
- + glob.glob('/path/*/*/*.jpg') \
- + glob.glob('/path/*/*/*/*.jpg') \
- + glob.glob('/path/*/*/*/*/*.jpg')
Python3的写法要清爽的多:
- found_images = glob.glob('/path/**/*.jpg', recursive=True)
事实上更好的用法是使用pathlib:
- found_images = pathlib.Path('/path/').glob('**/*.jpg')
Python 3之后print成为了函数,有了更多的扩展能力:
- In : print(*[1, 2, 3], sep='\t')
- 1 2 3
- In : [x if x % 3 else print('', x) for x in range(10)]
- 0
- 3
- 6
- 9
- Out: [None, 1, 2, None, 4, 5, None, 7, 8, None]
格式化字符串变量
- In : name = 'Fred'
- In : f'My name is {name}'
- Out: 'My name is Fred'
- In : from datetime import *
- In : date = datetime.now().date()
- In : f'{date} was on a {date:%A}'
- Out: '2018-01-17 was on a Wednesday'
- In : def foo():
- ....: return 20
- ....:
- In : f'result={foo()}'
- Out: 'result=20'
更严格的对比规范
下面这几种类型的用法在Python 3都是非法的:
- 3 < '3'
- 2 < None
- (3, 4) < (3, None)
- (4, 5) < [4, 5]
- sorted([2, '1', 3])
统一unicode的使用
这是很多人黑Python 2的一点,举个例子。在Python 2里面下面的结果很奇怪:
- In : s = '您好'
- In : print(len(s))
- 6
- In : print(s[:2])
- ?
Python 3就方便了:
- In : s = '您好'
- In : print(len(s))
- 2
- In : print(s[:2])
- 您好
合并字典
- In : x = dict(a=1, b=2)
- In : y = dict(b=3, d=4)
- In : z = {**x, **y}
- In : z
- Out: {'a': 1, 'b': 3, 'd': 4}
字典可排序
Python 3不再需要直接使用OrderedDict:
- In : {str(i):i for i in range(5)}
- Out: {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4}
相关文章
- 图像处理工具Python扩展库,你了解吗?
- 十个常用的损失函数解释以及Python代码实现
- 30 个数据科学工作中必备的 Python 包
- 如何在 Windows 上安装 Python
- 几行 Python 代码就可以提取数百个时间序列特征
- 使用Python快速搭建接口自动化测试脚本实战总结
- 哪种编程语言最适合开发网页抓取工具?
- 不要在 Python 中使用循环,这些方法其实更棒!
- 震惊!用Python探索《红楼梦》的人物关系!
- 如何最简单、通俗地理解Python模块?
- 酷炫,Python实现交通数据可视化!
- 为什么急于寻找Python的替代者?
- 30 个数据工程必备的Python 包
- 去字节面试被面这题能答上来吗?谈谈你对时间轮的理解?
- 火山引擎在行为分析场景下的 ClickHouse JOIN 优化
- 用Python爬取了某宝1166家月饼数据进行可视化分析,终于找到最好吃的月饼~
- 在 Linux 上试试这个基于 Python 的文件管理器
- Python列表解析式到底该怎么用?
- 如何快速把你的 Python 代码变为 API
- 十个Python初学者常犯的错误