The abstract base classes provided in the collections
module, particularly MutableSequence
, can be useful when implementing list-like classes. These are available in Python 2.6 and later.
With ABCs you can implement the "core" functionality of your class and it will provide the methods which logically depend on what you've defined.
For example, implementing __getitem__
in a collections.Sequence
-derived class will be enough to provide your class with __contains__
, __iter__
, and other methods.
You may still want to use a contained list object to do the heavy lifting.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…