最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

15個(gè)Pythonic的代碼示例(值得收藏)

 更新時(shí)間:2020年10月29日 11:12:11   投稿:zx  
這篇文章主要介紹了15個(gè)Pythonic的代碼示例(值得收藏),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

Python由于語(yǔ)言的簡(jiǎn)潔性,讓我們以人類思考的方式來(lái)寫代碼,新手更容易上手,老鳥(niǎo)更愛(ài)不釋手。

要寫出 Pythonic(優(yōu)雅的、地道的、整潔的)代碼,還要平時(shí)多觀察那些大牛代碼,Github 上有很多非常優(yōu)秀的源代碼值得閱讀,比如:requests、flask、tornado,這里小明收集了一些常見(jiàn)的 Pythonic 寫法,幫助你養(yǎng)成寫優(yōu)秀代碼的習(xí)慣。

01. 變量交換

Bad

tmp = a
a = b
b = tmp

Pythonic

a,b = b,a

02. 列表推導(dǎo)

Bad

my_list = []
for i in range(10):
  my_list.append(i*2)

Pythonic

my_list = [i*2 for i in range(10)]

03. 單行表達(dá)式

雖然列表推導(dǎo)式由于其簡(jiǎn)潔性及表達(dá)性,被廣受推崇。

但是有許多可以寫成單行的表達(dá)式,并不是好的做法。

Bad

print 'one'; print 'two'

if x == 1: print 'one'

if <complex comparison> and <other complex comparison>:
  # do something

Pythonic

print 'one'
print 'two'

if x == 1:
  print 'one'

cond1 = <complex comparison>
cond2 = <other complex comparison>
if cond1 and cond2:
  # do something

04. 帶索引遍歷

Bad

for i in range(len(my_list)):
  print(i, "-->", my_list[i])

Pythonic

for i,item in enumerate(my_list):
  print(i, "-->",item)

05. 序列解包

Pythonic

a, *rest = [1, 2, 3]
# a = 1, rest = [2, 3]

a, *middle, c = [1, 2, 3, 4]
# a = 1, middle = [2, 3], c = 4

06. 字符串拼接

Bad

letters = ['s', 'p', 'a', 'm']
s=""
for let in letters:
  s += let

Pythonic

letters = ['s', 'p', 'a', 'm']
word = ''.join(letters)

07. 真假判斷

Bad

if attr == True:
  print 'True!'

if attr == None:
  print 'attr is None!'

Pythonic

if attr:
  print 'attr is truthy!'

if not attr:
  print 'attr is falsey!'

if attr is None:
  print 'attr is None!'

08. 訪問(wèn)字典元素

Bad

d = {'hello': 'world'}
if d.has_key('hello'):
  print d['hello']  # prints 'world'
else:
  print 'default_value'

Pythonic

d = {'hello': 'world'}

print d.get('hello', 'default_value') # prints 'world'
print d.get('thingy', 'default_value') # prints 'default_value'

# Or:
if 'hello' in d:
  print d['hello']

09. 操作列表

Bad

a = [3, 4, 5]
b = []
for i in a:
  if i > 4:
    b.append(i)

Pythonic

a = [3, 4, 5]
b = [i for i in a if i > 4]
# Or:
b = filter(lambda x: x > 4, a)

Bad

a = [3, 4, 5]
for i in range(len(a)):
  a[i] += 3

Pythonic

a = [3, 4, 5]
a = [i + 3 for i in a]
# Or:
a = map(lambda i: i + 3, a)

10. 文件讀取

Bad

f = open('file.txt')
a = f.read()
print a
f.close() 

Pythonic

with open('file.txt') as f:
  for line in f:
    print line 

11. 代碼續(xù)行

Bad

my_very_big_string = """For a long time I used to go to bed early. Sometimes, \
  when I had put out my candle, my eyes would close so quickly that I had not even \
  time to say “I'm going to sleep.”"""

from some.deep.module.inside.a.module import a_nice_function, another_nice_function, \
  yet_another_nice_function 

Pythonic

my_very_big_string = (
  "For a long time I used to go to bed early. Sometimes, "
  "when I had put out my candle, my eyes would close so quickly "
  "that I had not even time to say “I'm going to sleep.”"
)

from some.deep.module.inside.a.module import (
  a_nice_function, another_nice_function, yet_another_nice_function) 

12. 顯式代碼

Bad

def make_complex(*args):
  x, y = args
  return dict(**locals())

Pythonic

def make_complex(x, y):
  return {'x': x, 'y': y}

13. 使用占位符

Pythonic

filename = 'foobar.txt'
basename, _, ext = filename.rpartition('.')

14. 鏈?zhǔn)奖容^

Bad

if age > 18 and age < 60:
  print("young man")

Pythonic

if 18 < age < 60:
  print("young man")

理解了鏈?zhǔn)奖容^操作,那么你應(yīng)該知道為什么下面這行代碼輸出的結(jié)果是 False

>>> False == False == True 
False

15. 三目運(yùn)算

這個(gè)保留意見(jiàn)。隨使用習(xí)慣就好。

Bad

if a > 2:
  b = 2
else:
  b = 1
#b = 2

Pythonic

a = 3  

b = 2 if a > 2 else 1
#b = 2 

參考文檔
http://docs.python-guide.org/en/latest/writing/style/
https://foofish.net/idiomatic_part2.html

到此這篇關(guān)于15個(gè)Pythonic的代碼示例(值得收藏)的文章就介紹到這了,更多相關(guān)Pythonic代碼內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Qt6中重大改變的QtMultimedia多媒體模塊實(shí)現(xiàn)

    Qt6中重大改變的QtMultimedia多媒體模塊實(shí)現(xiàn)

    本文主要介紹了Qt6中重大改變的QtMultimedia多媒體模塊實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • Python實(shí)現(xiàn)掃描局域網(wǎng)活動(dòng)ip(掃描在線電腦)

    Python實(shí)現(xiàn)掃描局域網(wǎng)活動(dòng)ip(掃描在線電腦)

    這篇文章主要介紹了Python實(shí)現(xiàn)掃描局域網(wǎng)活動(dòng)ip(掃描在線電腦),本文直接給出實(shí)現(xiàn)代碼,需要的朋友可以參考下
    2015-04-04
  • Python中super()的理解以及應(yīng)用場(chǎng)景實(shí)例

    Python中super()的理解以及應(yīng)用場(chǎng)景實(shí)例

    在python中關(guān)于類的定義可以分為兩種:老式類&新式類,在新式類中有這么一種方法super( ),下面這篇文章主要給大家介紹了關(guān)于Python中super()的理解以及應(yīng)用場(chǎng)景的相關(guān)資料,需要的朋友可以參考下
    2021-09-09
  • python 讀取.nii格式圖像實(shí)例

    python 讀取.nii格式圖像實(shí)例

    這篇文章主要介紹了python 讀取.nii格式圖像實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-07-07
  • 使用Python操作Jenkins的過(guò)程詳解

    使用Python操作Jenkins的過(guò)程詳解

    借助于Python中的python-jenkins模塊,我們可以輕松地編寫腳本來(lái)連接到Jenkins服務(wù)器,并執(zhí)行各種操作,如創(chuàng)建、刪除、構(gòu)建Jobs等,這種自動(dòng)化的方式不僅提高了效率,還使得CI/CD流程更加靈活和可控,本文介紹如何使用Python操作Jenkins的相關(guān)資料,需要的朋友可以參考下
    2024-05-05
  • Win10+python3.6+git運(yùn)行出現(xiàn)問(wèn)題的解決

    Win10+python3.6+git運(yùn)行出現(xiàn)問(wèn)題的解決

    這篇文章主要介紹了Win10+python3.6+git運(yùn)行出現(xiàn)問(wèn)題的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-06-06
  • Pycharm打開(kāi)已有項(xiàng)目配置python環(huán)境的方法

    Pycharm打開(kāi)已有項(xiàng)目配置python環(huán)境的方法

    這篇文章主要介紹了Pycharm打開(kāi)已有項(xiàng)目配置python環(huán)境的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-07-07
  • Django中的文件的上傳的幾種方式

    Django中的文件的上傳的幾種方式

    這篇文章主要介紹了Django中的文件的上傳的幾種方式,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-07-07
  • django自帶的server 讓外網(wǎng)主機(jī)訪問(wèn)方法

    django自帶的server 讓外網(wǎng)主機(jī)訪問(wèn)方法

    今天小編就為大家分享一篇django自帶的server 讓外網(wǎng)主機(jī)訪問(wèn)方法。具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-05-05
  • python實(shí)用的快捷語(yǔ)法技巧大全

    python實(shí)用的快捷語(yǔ)法技巧大全

    初識(shí)Python語(yǔ)言,覺(jué)得python滿足了我上學(xué)時(shí)候?qū)幊陶Z(yǔ)言的所有要求,下面這篇文章主要給大家介紹了關(guān)于python實(shí)用的快捷語(yǔ)法技巧的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-02-02

最新評(píng)論

万盛区| 营口市| 武隆县| 清镇市| 镇远县| 丘北县| 蓝山县| 盐亭县| 和平区| 东城区| 禄丰县| 大同县| 富裕县| 遂宁市| 宽甸| 贵州省| 平遥县| 南城县| 绍兴县| 南华县| 旬阳县| 呈贡县| 嘉兴市| 额济纳旗| 阿荣旗| 二连浩特市| 石门县| 文化| 冕宁县| 从江县| 东乡县| 修水县| 鄂托克旗| 蒙山县| 边坝县| 英山县| 蓝山县| 南皮县| 华亭县| 天峻县| 于都县|