Python 3特色用法:新特性汇总

新闻 后端
这篇文章灵感来源于一个新项目 A short guide on features of Python 3 for data scientists,这个项目列出来了作者使用Python 3用到的一些特性。

 这篇文章灵感来源于一个新项目 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'
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.

可迭代对象的解包

In : a, b, *rest = range(10)  # 学过lisp就很好懂了,相当于一个「everything else」 
In : a 
Out: 0 
In : b 
Out: 1 
In : rest 
Out: [23456789
 
In : *prev, next_to_last, last = range(10
In : prev, next_to_last, last 
Out: ([01234567], 89
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.

强制关键字参数

使用强制关键字参数会比使用位置参数表意更加清晰,程序也更加具有可读性,那么可以让这些参数强制使用关键字参数传递,可以将强制关键字参数放到某个 参数或者单个 后面就能达到这种效果:

In : def recv(maxsize, *, block): 
....: 
....:     pass 
....: 
 
In : recv(1024True
--------------------------------------------------------------------------- 
TypeError                                 Traceback (most recent call last) 
<ipython-input-49-8e61db2ef94bin <module>() 
----> 1 recv(1024True
 
TypeError: recv() takes 1 positional argument but 2 were given 
 
In : recv(1024, block=True
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.

通配符**

我们都知道在Python 2时不能直接通配递归的目录,需要这样:

found_images = \ 
    glob.glob('/path/*.jpg') \ 
  + glob.glob('/path/*/*.jpg') \ 
  + glob.glob('/path/*/*/*.jpg') \ 
  + glob.glob('/path/*/*/*/*.jpg') \ 
  + glob.glob('/path/*/*/*/*/*.jpg'
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.

Python3的写法要清爽的多:

found_images = glob.glob('/path/**/*.jpg', recursive=True
  • 1.

事实上更好的用法是使用pathlib:

found_images = pathlib.Path('/path/').glob('**/*.jpg'
  • 1.

print

Python 3之后print成为了函数,有了更多的扩展能力:

In : print(*[123], sep='\t'
1   2   3 
In : [x if x % 3 else print('', x) for x in range(10)] 
 0 
 3 
 6 
 9 
Out: [None12None45None78None
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.

格式化字符串变量

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' 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.

更严格的对比规范

下面这几种类型的用法在Python 3都是非法的:

3 < '3' 
2 < None 
(34) < (3None
(45) < [45
sorted([2'1'3]) 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.

统一unicode的使用

这是很多人黑Python 2的一点,举个例子。在Python 2里面下面的结果很奇怪:

In : s = '您好' 
 
In : print(len(s)) 
6 
 
In : print(s[:2]) 

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.

Python 3就方便了:

In : s = '您好' 
 
In : print(len(s)) 
2 
 
In : print(s[:2]) 
您好 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.

合并字典

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
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.

字典可排序

Python 3不再需要直接使用OrderedDict:

In : {str(i):i for i in range(5)} 
Out: {'0'0'1'1'2'2'3'3'4'4
  • 1.
  • 2.
责任编辑:张燕妮 来源: 推酷
相关推荐

2017-02-06 11:17:31

iOSiOS 10.3新特性

2009-02-04 17:33:24

ibmdwPython

2010-08-05 08:54:00

Flex优势

2024-01-15 00:30:04

Python 3语言版本

2009-09-24 10:22:38

Hibernate3新

2009-07-27 10:35:20

2011-04-11 09:11:42

GNOME 3

2021-12-10 14:53:17

微软Windows 11Windows

2013-06-27 09:35:26

Windows 8.1预览版特性

2021-08-18 16:06:27

iOS应用系统

2021-06-23 09:46:16

Python 3.10结构模式管理器

2010-10-12 09:52:02

ASP.NET MVC

2011-01-15 23:07:59

2009-03-13 09:54:35

HibernateHQLSQL

2023-06-19 08:05:17

RFCwebSpring

2013-06-27 13:01:58

Windows 8.1

2009-06-15 14:53:00

NetBeans 6.

2023-04-14 16:45:21

CSS前端CSS3

2022-07-14 08:22:48

Computedvue3

2011-11-18 13:25:48

HTML 5
点赞
收藏

51CTO技术栈公众号