序列类型

Python中的序列类型,序列类型可以使用for循环遍历。

序列类,序列是python中非常重要的协议,如何通过实现这个协议,将类变为序列类。

Python中的序列分类两个维度区分:

  • 容器序列:可以放置任意类型的数据。
  • 可变序列、不可变序列。

序列类型的一些协议

from collections import abc

跟容器相关的数据结构的抽象基类都是放到abc里的。

dict的abc继承关系

from collections.abc import Mapping, MutableMapping

dict属于 mapping 类型

from collections.abc import Mapping, MutableMapping
# dict属于 mapping 类型

a = {}

print(type(a))
print(isinstance(a, MutableMapping))

打印结果:
<class 'dict'>
True

上边代码中,a 并不是继承了MutableMapping,只是实现了MutableMapping中的一些魔法函数。

dict常用方法:

  • clear方法:清空dict
  • copy方法:返回浅拷贝


作者:kakaluot
链接:https://www.jianshu.com/p/9cc17e3d8c0c

相关文章:

  • 2021-10-23
  • 2021-06-24
  • 2022-03-08
  • 2021-11-13
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-27
猜你喜欢
  • 2022-12-23
  • 2021-11-08
  • 2021-06-08
  • 2021-12-27
  • 2021-11-17
  • 2022-12-23
  • 2021-09-04
相关资源
相似解决方案