
On Mon, Nov 29, 2021 at 6:01 AM Christopher Barker <pythonchb@gmail.com> wrote:
Hmm -- I jsut had a whacky idea:
As pointed out, adding a bunch of chaining methods to the Iterator ABC isn't really helpful, as it would A) potentially break / override existing classes that happen to use teh same names, and B) not provide anything for Iterators that don't subclass from the ABC.
But we *could* define a new ChainedIterator ABC. Anyone could then subclass from it to get the chaining behavior, and we could (potentially) use it for many of the iterators in the stdlib.
I'm not sure I'd even support this, but it's an idea.
I wouldn't support it. One of the great things about Python is that it's fairly easy to teach this progression: 1) Here's how you can do stuff in a generic way 2) Here's how you can make your own thing fit that generic way 3) Now your thing can be used by anyone else using #1 For example: 1) 1 + 2, "sp" + "am" 2) __add__ (don't worry about teaching __radd__ initially) 3) Demo() + 3 Or: 1) @lru_cache def foo(x, y) 2) def newdeco(f) 3) @newdeco def foo(x, y) Or use one with another: 1) with open(x) as f: ... 2) @contextlib.contextmanager def spaminate(x) 3) with spaminate(x) as spam: ... And this works because the simple approach IS the way to completely fit in with the protocol. Most Python protocols consist of *one* dunder method on a class. Some require two, or there are a couple that interact with each other (__eq__ and __hash__, for instance), but still, two methods isn't huge. Requiring that someone subclass an ABC as well as defining __iter__ would increase that complexity. Sometimes - in fact, often - you won't notice (because regular iteration still works), and if you subclass something else that subclasses it, no problem. But if you subclass something that happens to have a method name that collides, now you have a problem, and the order of inheritance will matter. But hey. Ideas are great, and figuring out a way to work within the existing structure would be awesome. I have a suspicion it's not possible, though, and it might just be that pipeline programming will end up needing syntactic support. ChrisA