[Python-ideas] __iter__ implies __contains__?

Antoine Pitrou solipsis at pitrou.net
Sun Oct 2 16:36:46 CEST 2011


On Mon, 03 Oct 2011 01:10:33 +1100
Steven D'Aprano <steve at pearwood.info> wrote:
> Antoine Pitrou wrote:
> > On Sat, 1 Oct 2011 22:30:21 -0400
> > Nick Coghlan <ncoghlan at gmail.com> wrote:
> >> On Sat, Oct 1, 2011 at 9:21 PM, Chris Rebert <pyideas at rebertia.com> wrote:
> >>> Actually, my suggestion was just that Sequence is one possible (but
> >>> clean) way to obtain the behavior; one could *of course* reimplement
> >>> the functionality without recourse to Sequence if they desired.
> >> But why would that would forcing everyone implementation standard
> >> sequences to reimplement the wheel be an improvement over the status
> >> quo?
> > 
> > You don't reinvent the wheel if you accept to inherit from abc.Sequence.
> 
> You shouldn't be forced to inherit from abc.Sequence to implement the 
> sequence protocols. The whole point of protocols is that they you don't 
> need inheritance to make them work, let alone buy into the ABC mindset.

Again, nobody said you had to inherit from abc.Sequence. It just
provides a convenience. If you prefer to implement everything by hand,
then fine. You already have __reversed__(), index() and count() to
write, so I'm not sure why __contains__() would be scary or annoying.






More information about the Python-ideas mailing list