这个说法是真的吗?
Python does not enforce a strict type on containers or variables. With this concept, developers can design a container to hold different types of data
我正在写一篇关于 Python 的文章,在 random site 上找到了这条语句,只是想知道是否有人可以为我澄清一下。
最佳答案
是的,这是真的。
除了设计一个容器,常规列表还可以容纳多种类型
>>> myList = [1, 3.14, 'string', {'key1':'value1'}, frozenset([5,2,2,3]), [1,2,3],(5+2j),(4.0,5.0), True, type(5)]
>>> myList
[1, 3.1400000000000001, 'string', {'key1': 'value1'}, frozenset([2, 3, 5]), [1, 2, 3], (5+2j), (4.0, 5.0), True, <type 'int'>]
>>> [type(x) for x in myList]
[<type 'int'>, <type 'float'>, <type 'str'>, <type 'dict'>, <type 'frozenset'>, <type 'list'>, <type 'complex'>, <type 'tuple'>, <type 'bool'>, <type 'type'>]
甚至 type
也是一个类型
关于Python 数据类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4642032/