Python 中的命名元組是內建元組資料類型的擴展,允許您為元組的元素賦予有意義的名稱。換句話說,命名元組是具有命名屬性的元組。是不是很酷?
它們是集合模組的一部分,提供了一種以更簡單的方式定義簡單、不可變的類別的方法。
等等,上課嗎?
是的,上課。
命名元組本質上是不可變的類別。
這就是發生的神奇之處:當您使用namedtuple建立命名元組時,結果不是元組本身的實例,而是繼承自元組的動態產生的類別。再說一遍,酷! !
讓我們看看這是如何運作的。
from collections import namedtuple P = namedtuple("Point", "x y")
當您執行 P = nametuple("Point", "x y") 時,您將建立一個名為 Point 的新類別(如namedtuple 的第一個參數所指定)。
namedtuple 函數在幕後使用 type 動態建立一個繼承自 tuple 的名為 Point 的新類別。這個新類別儲存在變數 P 中。
與類別一樣,類型就是型別。
> type(P) class 'type'
> class A: pass > type(A) class 'type'
namedtuple 的實例是什麼型別?
from collections import namedtuple P = namedtuple("Point", "x y") p = P(1,2) > print(type(p)) class '__main__.Point'
p 是 Point 類型的實例。但它也是一個元組:
> print(isinstance(p, tuple)) True
總結:
- P是由namedtuple動態產生的類別。
- P 的實例是 Point 類型的對象,它也是子類 元組。
最後一件事:
將namedtuple變數(我們稱為P)命名為與類型名稱(我們稱為Point)相同的名稱是很常見的,動態建立的類別:
from collections import namedtuple Point = namedtuple("Point", "x y")
我使用了不同的名稱來明確兩者之間的差異。
以上是Python 中的命名元組:它們是什麼型別?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

pythonisehybridmodeLofCompilation和interpretation:1)thepythoninterpretercompilesourcecececodeintoplatform- interpententbybytecode.2)thepythonvirtualmachine(pvm)thenexecutecutestestestestestesthisbytecode,ballancingEaseofuseEfuseWithPerformance。

pythonisbothinterpretedAndCompiled.1)它的compiledTobyTecodeForportabilityAcrosplatforms.2)bytecodeisthenInterpreted,允許fordingfordforderynamictynamictymictymictymictyandrapiddefupment,儘管Ititmaybeslowerthananeflowerthanancompiledcompiledlanguages。

在您的知識之際,而foroopsareideal insinAdvance中,而WhileLoopSareBetterForsituations則youneedtoloopuntilaconditionismet

ForboopSareSusedwhenthentheneMberofiterationsiskNownInAdvance,而WhileLoopSareSareDestrationsDepportonAcondition.1)ForloopSareIdealForiteratingOverSequencesLikelistSorarrays.2)whileLeleLooleSuitableApeableableableableableableforscenarioscenarioswhereTheLeTheLeTheLeTeLoopContinusunuesuntilaspecificiccificcificCondond

pythonisnotpuroly interpred; itosisehybridablectofbytecodecompilationandruntimeinterpretation.1)PythonCompiLessourceceCeceDintobyTecode,whitsthenexecececected bytybytybythepythepythepythonvirtirtualmachine(pvm).2)

concatenateListSinpythonWithTheSamelements,使用:1)operatoTotakeEpduplicates,2)asettoremavelemavphicates,or3)listcompreanspherensionforcontroloverduplicates,每個methodhasdhasdifferentperferentperferentperforentperforentperforentperfornceandordorimplications。

pythonisanterpretedlanguage,offeringosofuseandflexibilitybutfacingperformancelanceLimitationsInCricapplications.1)drightingedlanguageslikeLikeLikeLikeLikeLikeLikeLikeThonexecuteline-by-line,允許ImmediaMediaMediaMediaMediaMediateFeedBackAndBackAndRapidPrototypiD.2)compiledLanguagesLanguagesLagagesLikagesLikec/c thresst

Useforloopswhenthenumberofiterationsisknowninadvance,andwhileloopswheniterationsdependonacondition.1)Forloopsareidealforsequenceslikelistsorranges.2)Whileloopssuitscenarioswheretheloopcontinuesuntilaspecificconditionismet,usefulforuserinputsoralgorit


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

Atom編輯器mac版下載
最受歡迎的的開源編輯器

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!

Dreamweaver CS6
視覺化網頁開發工具

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中