What exactly are “containers” in python? (And what are all the python container types?)

前端 未结 3 415
礼貌的吻别
礼貌的吻别 2020-12-12 11:22

The python documentation frequently speaks of \"containers\". E.g. :

If check_circular is False (default: True), then the circular reference check f

3条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-12 11:45

    According to http://docs.python.org/dev/library/collections.abc.html#module-collections.abc, the most general definition of a container is simply an object that implements __contains__. In general, Python concepts like "container" or "sequence" are not defined abstractly; they are "duck-typed" by their behavior. That is, a container is something that you can use the in operator on.

    The Python builtin container types are tuple, list, dict, set, frozenset and str and unicode (or bytes and str in Python 3), as well as a couple other constructs that are technically types but are not commonly used outside of specific contexts (e.g., buffer objects and xrange objects). Additional container types are provided in the collections module.

提交回复
热议问题