开发者

Lists in Python

Is the List in py开发者_开发知识库thon homogeneous or heterogeneous?


>>> def a(): pass
>>> lst=[1,'one',{1:'one'},a,[1,1],(1,),True,set((1,))]
>>> for each in lst:
...    print type(each), str(each)
... 
<type 'int'> 1
<type 'str'> one
<type 'dict'> {1: 'one'}
<type 'function'> <function a at 0x100496938>
<type 'list'> [1, 1]
<type 'tuple'> (1,)
<type 'bool'> True
<type 'set'> set([1])

Any questions?


Lists in Python can be heterogeneous, but by the general convention it is preferable that they only contain homogeneous elements. Python tuples are the natural data structure for heterogeneous sequences.

Of course you can argue that both tuples and lists are just homogeneous sequences of Python objects, but that is a misleading oversimplification and doesn't add any value.

Since tuples are immutable and lists are mutable you could also argue that mutability is the real distinction between them. However, that is not how they were intended. More on this can be found in this question.


The List in Python is heterogeneous - the same list can accept objects of various different types.

There is a snippet here which gives you a homogeneous list in Python. No idea how that piece of code would perform however.


Please try a simple

a=[1, "a"]

and see if it throws an error before asking a question.

Btw, it does not.


One more argument for homogeneity of lists: the PEP 484 and the new typing module. The items of tuple can be individually typed with Tuple:

Tuple, used by listing the element types, for example Tuple[int, int, str]. The empty tuple can be typed as Tuple[()]. Arbitrary-length homogeneous tuples can be expressed using one type and ellipsis, for example Tuple[int, ...]. (The ... here are part of the syntax, a literal ellipsis.) [reference]

>>> import typing
>>> typing.Tuple[str, int, float]
typing.Tuple[str, int, float]

However a list, via typing.List only accepts a single type parameter:

List, used as List[element_type]

>>> typing.List[str, int, float]
Traceback (most recent call last):
  ...
TypeError: Too many parameters for typing.List<~T>; actual 3, expected 1


It depends if you talk about the direct elements of a list or its indirect elements.

I believe that a list is in reality a list of references, not of objects, and that when the interpeter has to use a list, it knows that in fact it must manipulate the objects to which the references are pointing, not the references themselves: that's the "execution model" of Python, I think.

  • What I call indirect element of a list are the objects that constitute the value of the list.

  • What I call direct elements are the references to these objects that constitutes the implementation of the list.

So:

  • considered as a list of references, a list is homogenous: all the elements are variables, in the plain sense of variable = chunk of memory whose value can change (while the value of an object never changes)

  • considered as a list of the objects pointed by the references, a list is heterogenous. The innuendo being that their TYPES are heterogenous.

That's my understanding. I don't know if I am fully right.


Your question does not make much sense since everything in Python is an object. And a list can hold arbitrary instances of objects if needed.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜