亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

數據類型`NoneType`比較?

數據類型`NoneType`比較?

LEATH 2021-09-25 16:25:49
我正在嘗試1-65535使用urlparse.棘手的部分是,如果 URL 中沒有端口號,urlparse(url).port則標識為NoneType因此,我嘗試根據數據類型進行簡單的比較,但并沒有像您在此示例中看到的那樣真正起作用。如果我NoneType用作數據類型,elif type(u.port) == NoneType:我收到錯誤NameError: name 'NoneType' is not defined我沒有使用,validators因為它無法正確驗證端口號。TCP/UDP 端口號范圍從 1-65535 開始。但是,validators無法識別端口1-9并且仍然接受大于65535.代碼from urllib.parse import urlparsedef test(url):    global u    u = urlparse(url)    print(' Port      : ', u.port)    print(' Data Type : u.port %s\n'% type(u.port))for url in ['example.com', 'example.com:1', 'http://example.com:1']:    print(url)    test(url)    if type(u.port) == int:        print(' Integer')    elif type(u.port) == None:        print(' None')    # elif type(u.port) == NoneType:        # NameError: name 'NoneType' is not defined    #   print(' None')輸出example.com Port      :  None Data Type : u.port <class 'NoneType'>example.com:1 Port      :  None Data Type : u.port <class 'NoneType'>http://example.com:1 Port      :  1 Data Type : u.port <class 'int'>    Integer
查看完整描述

3 回答

?
守候你守候我

TA貢獻1802條經驗 獲得超10個贊

不要比較要檢查的類型,請None使用:


elif u.port is None:

    print(' None')


查看完整回答
反對 回復 2021-09-25
?
尚方寶劍之說

TA貢獻1788條經驗 獲得超4個贊

您還可以通過以下方式檢查 NoneType:


elif isinstance(u.port, type(None)):

    print(' None')


查看完整回答
反對 回復 2021-09-25
  • 3 回答
  • 0 關注
  • 242 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號