Skip to main content
Add more information and expand on list-like magic functions
Source Link
CAD97
  • 1.9k
  • 1
  • 13
  • 35

I agree that default arguments can (and should) be used for the backing list of your class.

In addition, consider inheriting from collections.abc.Sequence and delegating __getitem__ and __len__ to the backing list. (Add other list-like methods as necessary.) This will make sure your class acts as a well-behaved list-like.

In order to qualify as an Iterable, you must define __iter__ or __getitem__. In order to qualify as a Sequence you must be an Iterable that provides __getitem__ and __len__. It's up to you how much functionality you want to provide, but the Sequence ABC exists for a reason.

I agree that default arguments can (and should) be used for the backing list of your class.

In addition, consider inheriting from collections.abc.Sequence and delegating __getitem__ to the backing list. (Add other list-like methods as necessary.) This will make sure your class acts as a well-behaved list-like.

I agree that default arguments can (and should) be used for the backing list of your class.

In addition, consider inheriting from collections.abc.Sequence and delegating __getitem__ and __len__ to the backing list. (Add other list-like methods as necessary.) This will make sure your class acts as a well-behaved list-like.

In order to qualify as an Iterable, you must define __iter__ or __getitem__. In order to qualify as a Sequence you must be an Iterable that provides __getitem__ and __len__. It's up to you how much functionality you want to provide, but the Sequence ABC exists for a reason.

Source Link
CAD97
  • 1.9k
  • 1
  • 13
  • 35

I agree that default arguments can (and should) be used for the backing list of your class.

In addition, consider inheriting from collections.abc.Sequence and delegating __getitem__ to the backing list. (Add other list-like methods as necessary.) This will make sure your class acts as a well-behaved list-like.