99999久久久久久亚洲,欧美人与禽猛交狂配,高清日韩av在线影院,一个人在线高清免费观看,啦啦啦在线视频免费观看www

熱線電話:13121318867

登錄
首頁(yè)精彩閱讀絕對(duì)福音!Python3.10的這項(xiàng)改進(jìn)太棒了,治好了我眼睛
絕對(duì)福音!Python3.10的這項(xiàng)改進(jìn)太棒了,治好了我眼睛
2021-06-10
收藏

來源:麥?zhǔn)寰幊?

作者:麥?zhǔn)?

一、Python 3.10

2021年6月9號(hào),Python官方發(fā)布了3.10的新功能介紹。

絕對(duì)福音!Python3.10的這項(xiàng)改進(jìn)太棒了,治好了我眼睛

以往每次有新的版本,我都替Python開發(fā)者叫苦:又有新的版本,又要學(xué)新的東西,又要修改程序,命苦??!

但這次看了以后,我禁不住連聲叫好:這個(gè)太棒了!福音!

二、主要功能改進(jìn)

先簡(jiǎn)單說一下3.10的幾項(xiàng)核心改進(jìn):

1. 在with ... as語(yǔ)法中支持小括號(hào),可以一行創(chuàng)建多個(gè)環(huán)境管理器。

with (
    CtxManager1() as example1,
    CtxManager2() as example2,
    CtxManager3() as example3,
):

關(guān)于環(huán)境管理器,后面可以單獨(dú)發(fā)文討論細(xì)節(jié)。

2. 更好的錯(cuò)誤提示

這就是讓我連連叫好的功能,這也是本文后面討論的重點(diǎn)。

3. 支持match ... case功能,也就是其他編程語(yǔ)言的swith ... case:

    match status:
        case 400:
            return "Bad request"         case 404:
            return "Not found"         case 418:
            return "I'm a teapot"         case _:
            return "Something's wrong with the Internet" 

這個(gè)后面可以單獨(dú)發(fā)文討論細(xì)節(jié)。

4. 其他功能

還有一些其他小的功能改進(jìn):

  • 跟蹤調(diào)試中提供更準(zhǔn)確可靠的行數(shù)
  • 幾個(gè)關(guān)于類型(type hint)的改進(jìn),比如支持類型的union操作:X | Y表示類型X或者Y
  • asyncio, base64等幾十個(gè)模塊有一些細(xì)小的改動(dòng)
  • 其他的一些細(xì)小的語(yǔ)言改動(dòng),比如int加了一個(gè)新的方法int.bit_count()

這些細(xì)節(jié)基本不會(huì)影響你現(xiàn)有的代碼,有興趣的同學(xué)可以點(diǎn)本文的閱讀原文,查看完整的英文原文。

三、更好的錯(cuò)誤提示

讓我拍手叫絕的這個(gè)功能就是更好的錯(cuò)誤提示。

編程學(xué)習(xí)者,尤其是新手,會(huì)碰到各種各樣的編程錯(cuò)誤,

而這些錯(cuò)誤的提示又不友好,甚至有些誤導(dǎo)!

錯(cuò)誤就在我們面前,我們瞪著眼睛卻看不見,這種情況我稱為眼瞎問題。

就算我這樣的老司機(jī),也時(shí)不時(shí)的會(huì)碰到瞎眼問題。

3.10對(duì)錯(cuò)誤提示有了很大的改進(jìn),可以說在一定程度上能挽救我們眼瞎問題

這個(gè)改進(jìn)涵蓋了:語(yǔ)法錯(cuò)誤,縮進(jìn)錯(cuò)誤,屬性錯(cuò)誤,名稱錯(cuò)誤等。

我們一個(gè)個(gè)看,建議先肉眼看一下,錯(cuò)誤在哪里,我們來一個(gè)找錯(cuò)誤游戲。

1. 語(yǔ)法錯(cuò)誤

代碼1:

expected = {9: 1, 18: 2, 19: 2, 27: 3, 28: 3, 29: 3, 36: 4, 37: 4,             38: 4, 39: 4, 45: 5, 46: 5, 47: 5, 48: 5, 49: 5, 54: 6, some_other_code = foo() 

以前的錯(cuò)誤提示:

有語(yǔ)法錯(cuò)誤。還把錯(cuò)誤的行數(shù)說錯(cuò)了,這個(gè)就很誤導(dǎo)了!

File "example.py", line 3     some_other_code = foo()
                    ^ SyntaxError: invalid syntax

現(xiàn)在的提示要好多了:

大括號(hào)沒有關(guān)閉。這個(gè)就很自白了!

File "example.py", line 1     expected = {9: 1, 18: 2, 19: 2, 27: 3, 28: 3, 29: 3, 36: 4, 37: 4,                ^ SyntaxError: '{' was never closed 

代碼2:

>>> foo(x, z for z in range(10), t, w)
  File "<stdin>", line 1     foo(x, z for z in range(10), t, w)
           ^
SyntaxError: Generator expression must be parenthesized

這段代碼是因?yàn)?span style="color:#EF7060;">z for z in range(10)應(yīng)該加上括號(hào),提示也還比較正常。

但是3.10給出了更好的提示,直接把要加括號(hào)的代碼段給標(biāo)記出來了:

>>> foo(x, z for z in range(10), t, w)
  File "<stdin>", line 1     foo(x, z for z in range(10), t, w)
           ^^^^^^^^^^^^^^^^^^^^
SyntaxError: Generator expression must be parenthesized

是不是很直白?到這里是不是應(yīng)該給本文點(diǎn)個(gè)贊?或者答應(yīng)我,看完以后點(diǎn)個(gè)贊,謝謝!

下面幾段我們就不作對(duì)比了,直接給出3.10中友好的錯(cuò)誤提示

  • 少了冒號(hào)
>>> if rocket.position > event_horizon
  File "<stdin>", line 1     if rocket.position > event_horizon
                                      ^ SyntaxError: expected ':' 
  • 元組少了括號(hào)
>>> {x,y for x,y in zip('abcd''1234')}
  File "<stdin>", line 1     {x,y for x,y in zip('abcd''1234')}
     ^ SyntaxError: did you forget parentheses around the comprehension target?
  • 字典少了逗號(hào)
>>> items = { ... x: 1, ... y: 2 ... z: 3,   File "<stdin>", line 3     y: 2        ^ SyntaxError: invalid syntax. Perhaps you forgot a comma? 
  • 異常捕捉的時(shí)候少了逗號(hào)
>>> try:
...     build_dyson_sphere()
... except NotEnoughScienceError, NotEnoughResourcesError:
  File "<stdin>", line 3     except NotEnoughScienceError, NotEnoughResourcesError:
           ^
SyntaxError: multiple exception types must be parenthesized
  • 字典少了value
>>> values = {
... x: 1,
... y: 2,
... z: ... }
  File "<stdin>", line 4     z:      ^ SyntaxError: expression expected after dictionary key and ':' >>> values = {x:1y:2, z w:3}
  File "<stdin>", line 1     values = {x:1y:2, z w:3}
                        ^ SyntaxError: ':' expected after dictionary key
  • try少了except或者finally
>>> try ...     x = 2 ... something = 3   File "<stdin>", line 3     something  = 3     ^^^^^^^^^ SyntaxError: expected 'except' or 'finally' block
  • 比較的時(shí)候用了=,而不是==。這個(gè)太贊了!
>>> if rocket.position = event_horizon:   File "<stdin>", line 1     if rocket.position = event_horizon:                        ^ SyntaxError: cannot assign to attribute here. Maybe you meant '==' instead of '='?
  • 在f-string中用了星號(hào)*
>>> f"Black holes {*all_black_holes} and revelations"   File "<stdin>", line 1     (*all_black_holes)
     ^ SyntaxError: f-string: cannot use starred expression here

2. 縮進(jìn)錯(cuò)誤

縮進(jìn)錯(cuò)誤是小白常見錯(cuò)誤,現(xiàn)在有救啦,提示很友好:

>>def foo():
...    if lel: ...    x = 2   File "<stdin>", line 3     x = 2     ^ IndentationError: expected an indented block after 'if' statement in line 2 

3. 屬性錯(cuò)誤

用錯(cuò)了屬性,不光告訴你錯(cuò)誤,還給你一些可能的選擇,簡(jiǎn)直有點(diǎn)人工智能的味道了。

>>> collections.namedtoplo
Traceback (most recent call last):
  File "<stdin>", line 1in <module> AttributeError: module 'collectionshas no attribute 'namedtoplo'. Did you meannamedtuple? 

4. 命名錯(cuò)誤

同樣的,命名錯(cuò)誤也會(huì)給出一個(gè)可能的選擇:

>>> schwarzschild_black_hole = None >>> schwarschild_black_hole
Traceback (most recent call last):
  File "<stdin>", line 1in <module> NameError: name 'schwarschild_black_hole' is not defined. Did you mean: schwarzschild_black_hole?

四、結(jié)束語(yǔ)

這是python 3.10中最讓我眼睛一亮的一項(xiàng)功能,相信可以解救很多和我一樣有瞎眼問題的同學(xué)。

Python 3.10的match..case功能也可以算是一大改進(jìn),另外context manager的功能也很重要,如果大家有興趣,請(qǐng)留言,點(diǎn)贊,后面我再專門發(fā)文介紹這兩個(gè)知識(shí)點(diǎn)。

最后建議:搜藏本文,認(rèn)真學(xué)些這些錯(cuò)誤提示。應(yīng)該可以節(jié)省你很多找錯(cuò)誤的時(shí)間。

數(shù)據(jù)分析咨詢請(qǐng)掃描二維碼

若不方便掃碼,搜微信號(hào):CDAshujufenxi

數(shù)據(jù)分析師資訊
更多

OK
客服在線
立即咨詢
客服在線
立即咨詢
') } function initGt() { var handler = function (captchaObj) { captchaObj.appendTo('#captcha'); captchaObj.onReady(function () { $("#wait").hide(); }).onSuccess(function(){ $('.getcheckcode').removeClass('dis'); $('.getcheckcode').trigger('click'); }); window.captchaObj = captchaObj; }; $('#captcha').show(); $.ajax({ url: "/login/gtstart?t=" + (new Date()).getTime(), // 加隨機(jī)數(shù)防止緩存 type: "get", dataType: "json", success: function (data) { $('#text').hide(); $('#wait').show(); // 調(diào)用 initGeetest 進(jìn)行初始化 // 參數(shù)1:配置參數(shù) // 參數(shù)2:回調(diào),回調(diào)的第一個(gè)參數(shù)驗(yàn)證碼對(duì)象,之后可以使用它調(diào)用相應(yīng)的接口 initGeetest({ // 以下 4 個(gè)配置參數(shù)為必須,不能缺少 gt: data.gt, challenge: data.challenge, offline: !data.success, // 表示用戶后臺(tái)檢測(cè)極驗(yàn)服務(wù)器是否宕機(jī) new_captcha: data.new_captcha, // 用于宕機(jī)時(shí)表示是新驗(yàn)證碼的宕機(jī) product: "float", // 產(chǎn)品形式,包括:float,popup width: "280px", https: true // 更多配置參數(shù)說明請(qǐng)參見:http://docs.geetest.com/install/client/web-front/ }, handler); } }); } function codeCutdown() { if(_wait == 0){ //倒計(jì)時(shí)完成 $(".getcheckcode").removeClass('dis').html("重新獲取"); }else{ $(".getcheckcode").addClass('dis').html("重新獲取("+_wait+"s)"); _wait--; setTimeout(function () { codeCutdown(); },1000); } } function inputValidate(ele,telInput) { var oInput = ele; var inputVal = oInput.val(); var oType = ele.attr('data-type'); var oEtag = $('#etag').val(); var oErr = oInput.closest('.form_box').next('.err_txt'); var empTxt = '請(qǐng)輸入'+oInput.attr('placeholder')+'!'; var errTxt = '請(qǐng)輸入正確的'+oInput.attr('placeholder')+'!'; var pattern; if(inputVal==""){ if(!telInput){ errFun(oErr,empTxt); } return false; }else { switch (oType){ case 'login_mobile': pattern = /^1[3456789]\d{9}$/; if(inputVal.length==11) { $.ajax({ url: '/login/checkmobile', type: "post", dataType: "json", data: { mobile: inputVal, etag: oEtag, page_ur: window.location.href, page_referer: document.referrer }, success: function (data) { } }); } break; case 'login_yzm': pattern = /^\d{6}$/; break; } if(oType=='login_mobile'){ } if(!!validateFun(pattern,inputVal)){ errFun(oErr,'') if(telInput){ $('.getcheckcode').removeClass('dis'); } }else { if(!telInput) { errFun(oErr, errTxt); }else { $('.getcheckcode').addClass('dis'); } return false; } } return true; } function errFun(obj,msg) { obj.html(msg); if(msg==''){ $('.login_submit').removeClass('dis'); }else { $('.login_submit').addClass('dis'); } } function validateFun(pat,val) { return pat.test(val); }