成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

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新增的模塊,讓你更方便的處理路徑相關的工作。

  1. In : from pathlib import Path 
  2. In : Path.home() 
  3. Out: PosixPath('/Users/dongweiming')  # 用戶目錄 
  4. In : path = Path('/user'
  5. In : path / 'local'  # 非常直觀 
  6. Out: PosixPath('/user/local'
  7. In : str(path / 'local' / 'bin'
  8. Out: '/user/local/bin' 
  9.  
  10. In : f = Path('example.txt'
  11. In : f.write_bytes('This is the content'.encode('utf-8')) 
  12. Out[16]: 19 
  13.  
  14. In : with f.open('r', encoding='utf-8') as handle:  # open現在是方法了 
  15. ....:         print('read from open(): {!r}'.format(handle.read())) 
  16. ....: 
  17. read from open(): 'This is the content' 
  18.  
  19. In : p = Path('touched'
  20. In : p.exists()  # 集成了多個常用方法 
  21. Out: False 
  22. In : p.touch() 
  23. In : p.exists() 
  24. Out: True 
  25.  
  26. In : p.with_suffix('.jpg'
  27. Out: PosixPath('touched.jpg'
  28. In : p.is_dir() 
  29. Out: False 
  30. In : p.joinpath('a''b'
  31. Out: PosixPath('touched/a/b'

可迭代對象的解包

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

強制關鍵字參數

使用強制關鍵字參數會比使用位置參數表意更加清晰,程序也更加具有可讀性,那么可以讓這些參數強制使用關鍵字參數傳遞,可以將強制關鍵字參數放到某個 參數或者單個 后面就能達到這種效果:

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

通配符**

我們都知道在Python 2時不能直接通配遞歸的目錄,需要這樣:

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

Python3的寫法要清爽的多:

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

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

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

print

Python 3之后print成為了函數,有了更多的擴展能力:

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

格式化字符串變量

  1. In : name = 'Fred' 
  2. In : f'My name is {name}' 
  3. Out: 'My name is Fred' 
  4.  
  5. In : from datetime import * 
  6. In : date = datetime.now().date() 
  7. In : f'{date} was on a {date:%A}' 
  8. Out: '2018-01-17 was on a Wednesday' 
  9.  
  10. In : def foo(): 
  11. ....:     return 20 
  12. ....: 
  13. In : f'result={foo()}' 
  14. Out: 'result=20' 

更嚴格的對比規范

下面這幾種類型的用法在Python 3都是非法的:

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

統一unicode的使用

這是很多人黑Python 2的一點,舉個例子。在Python 2里面下面的結果很奇怪:

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

Python 3就方便了:

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

合并字典

  1. In : x = dict(a=1, b=2
  2. In : y = dict(b=3, d=4
  3. In : z = {**x, **y} 
  4. In : z 
  5. Out: {'a'1'b'3'd'4

字典可排序

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

  1. In : {str(i):i for i in range(5)} 
  2. Out: {'0'0'1'1'2'2'3'3'4'4
責任編輯:張燕妮 來源: 推酷
相關推薦

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-07-27 10:35:20

2009-09-24 10:22:38

Hibernate3新

2011-04-11 09:11:42

GNOME 3

2021-12-10 14:53:17

微軟Windows 11Windows

2021-06-23 09:46:16

Python 3.10結構模式管理器

2021-08-18 16:06:27

iOS應用系統

2013-06-27 09:35:26

Windows 8.1預覽版特性

2010-10-12 09:52:02

ASP.NET MVC

2009-03-13 09:54:35

HibernateHQLSQL

2011-01-15 23:07:59

2023-06-19 08:05:17

RFCwebSpring

2013-06-27 13:01:58

Windows 8.1

2023-04-14 16:45:21

CSS前端CSS3

2009-06-15 14:53:00

NetBeans 6.

2025-04-16 10:03:40

開發Spring應用程序

2022-07-14 08:22:48

Computedvue3
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 二区亚洲| 欧美精品1区2区 | 中文字幕亚洲一区 | 91爱啪啪 | 日韩欧美一区二区三区免费观看 | 国产免费看 | 欧美二区在线 | 91精品久久久久久久久中文字幕 | 99久久中文字幕三级久久日本 | 亚洲精品日韩在线 | 欧美一区二区三区视频 | 夜夜夜夜夜夜曰天天天 | 罗宾被扒开腿做同人网站 | av香蕉 | 欧美一区二区三区 | 国产精品一区在线观看你懂的 | 国产99久久精品一区二区永久免费 | 国产高清视频在线播放 | 欧美一区二区三区视频 | 免费黄色的网站 | 在线小视频 | 97伦理最新伦理 | 国产高清久久 | 成人在线免费电影 | 国产成人精品视频在线观看 | 亚洲精品一区二区二区 | 国产精品久久久久久久久久久久午夜片 | 精品一区二区三区不卡 | 国产黄色精品在线观看 | 中文字幕1区2区3区 亚洲国产成人精品女人久久久 | 欧美在线免费 | 成人免费淫片aa视频免费 | 国产精品久久久久久久免费观看 | 欧美日韩1区2区 | 一本一道久久a久久精品蜜桃 | 蜜臀网站 | www.蜜桃av| 久久久这里都是精品 | 波多野结衣电影一区 | 日韩aⅴ在线观看 | 福利视频一区二区 |