日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
Python3特色用法:新特性匯總

 這篇文章靈感來(lái)源于一個(gè)新項(xiàng)目 A short guide on features of Python 3 for data scientists,這個(gè)項(xiàng)目列出來(lái)了作者使用Python 3用到的一些特性。正巧我最近也想寫一篇介紹Python 3(特指Python 3.6+)特色用法的文章。開始吧!

成都創(chuàng)新互聯(lián)-成都網(wǎng)站建設(shè)公司,專注網(wǎng)站建設(shè)、成都網(wǎng)站建設(shè)、網(wǎng)站營(yíng)銷推廣,空間域名,虛擬主機(jī),成都網(wǎng)站托管有關(guān)企業(yè)網(wǎng)站制作方案、改版、費(fèi)用等問題,請(qǐng)聯(lián)系成都創(chuàng)新互聯(lián)。

pathlib模塊

pathlib模塊是Python 3新增的模塊,讓你更方便的處理路徑相關(guān)的工作。

 
 
 
 
  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. In : f = Path('example.txt')
  10. In : f.write_bytes('This is the content'.encode('utf-8'))
  11. Out[16]: 19
  12. In : with f.open('r', encoding='utf-8') as handle:  # open現(xiàn)在是方法了
  13. ....:         print('read from open(): {!r}'.format(handle.read()))
  14. ....:
  15. read from open(): 'This is the content'
  16. In : p = Path('touched')
  17. In : p.exists()  # 集成了多個(gè)常用方法
  18. Out: False
  19. In : p.touch()
  20. In : p.exists()
  21. Out: True
  22. In : p.with_suffix('.jpg')
  23. Out: PosixPath('touched.jpg')
  24. In : p.is_dir()
  25. Out: False
  26. In : p.joinpath('a', 'b')
  27. Out: PosixPath('touched/a/b')

可迭代對(duì)象的解包

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

強(qiáng)制關(guān)鍵字參數(shù)

使用強(qiáng)制關(guān)鍵字參數(shù)會(huì)比使用位置參數(shù)表意更加清晰,程序也更加具有可讀性,那么可以讓這些參數(shù)強(qiáng)制使用關(guān)鍵字參數(shù)傳遞,可以將強(qiáng)制關(guān)鍵字參數(shù)放到某個(gè) 參數(shù)或者單個(gè) 后面就能達(dá)到這種效果:

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

通配符**

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

 
 
 
 
  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)

事實(shí)上更好的用法是使用pathlib:

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

print

Python 3之后print成為了函數(shù),有了更多的擴(kuò)展能力:

 
 
 
 
  1. In : print(*[1, 2, 3], 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: [None, 1, 2, None, 4, 5, None, 7, 8, None]

格式化字符串變量

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

更嚴(yán)格的對(duì)比規(guī)范

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

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

統(tǒng)一unicode的使用

這是很多人黑Python 2的一點(diǎn),舉個(gè)例子。在Python 2里面下面的結(jié)果很奇怪:

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

Python 3就方便了:

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

合并字典

 
 
 
 
  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}

網(wǎng)站題目:Python3特色用法:新特性匯總
網(wǎng)站鏈接:http://www.5511xx.com/article/dhjdiog.html