On Fri, Nov 26, 2021 at 4:12 PM Eric V. Smith <[email protected]> wrote:
> Another note: I'm not recommending it, but we could add a bunch of things > to the Iterator ABC, and then it could be available everywhere. > > Is that true? I'm genuinely curious. > > I have lots of code with is the logical equivalent of: > > class Foo: > def __init__(self, s): > self.s = s > self.idx = -1 > > def __iter__(self): > return self > > def __next__(self): > self.idx += 1 > if self.idx < len(self.s): > return self.s[self.idx] > raise StopIteration() > > Would adding something to the Iterator ABC really also add it to my class > Foo? > No, but if you subclasses from the ABC it would. Python ABCs are a mysterious beast. Python itself is mostly Duck Typed. So you example above it a full fledged Iterator. In this case, ABCs serve primarily as formal documentation. But they are a bit more than that, as some of them provided non-abstract functionality, that you can get by subclassing from them. And some type checking systems will check the presence of attributes in the ABCs, so that, for instance, your example would type check as an Iterator. -CHB Eric > _______________________________________________ > Python-ideas mailing list -- [email protected] > To unsubscribe send an email to [email protected] > https://mail.python.org/mailman3/lists/python-ideas.python.org/ > Message archived at > https://mail.python.org/archives/list/[email protected]/message/QKV2QNBFICYGYI7Z3PQLWI76Q25VZSQ7/ > Code of Conduct: http://python.org/psf/codeofconduct/ > -- Christopher Barker, PhD (Chris) Python Language Consulting - Teaching - Scientific Software Development - Desktop GUI and Web Development - wxPython, numpy, scipy, Cython
_______________________________________________ Python-ideas mailing list -- [email protected] To unsubscribe send an email to [email protected] https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/[email protected]/message/WOIBU2DQ2IMN4X4Y5YFR2PZYUBHBAK5D/ Code of Conduct: http://python.org/psf/codeofconduct/
