標籤:

有人反覆問Python container 是啥?

container 英文是容器,望文生義,是包含其它數據類型的一種數據結構或數據類型。

官方文檔,確實是這樣寫的。

Some objects contain references to other objects; these are called containers.

Python 容器的數據類型有如下:

list,nset,ndictionary,nOrderedDictionarynbytearraynarraynstring,nfrozenset,ntuple,nbytesn

collections — Container datatypes

namedtuple()tfactory function for creating tuple subclasses with named fieldsndequetlist-like container with fast appends and pops on either endnChainMaptdict-like class for creating a single view of multiple mappingsnCountertdict subclass for counting hashable objectsnOrderedDicttdict subclass that remembers the order entries were addedndefaultdicttdict subclass that calls a factory function to supply missing valuesnUserDicttwrapper around dictionary objects for easier dict subclassingnUserListtwrapper around list objects for easier list subclassingnUserStringtwrapper around string objects for easier string subclassingn

凡是有__iter__ 屬性的數據類型都是可以迭代的。

In [10]: lst = range(10)nnIn [11]: hasattr(lst, __iter__)nOut[11]: TruennIn [12]: s = "黃哥Python"nnIn [13]: hasattr(s, __iter__)nOut[13]: Truen

部分免費python免費視頻

pythonpeixun/article

216小時學會python

github.com/pythonpeixun
推薦閱讀:

TAG:Python |