I don’t know how well this idea fits Python overall, as it’s inspired by Apache Spark’s DataFrame API (which was, in turn, inspired by the DataFrame concept in R and Pandas).
The main benefit of being able to call methods like these on iterables is that it lets you chain multiple methods together in a way that’s easy to read, where the output of one method is piped to the input of the next method in the chain. I have seen this style of API called a fluent interface. It’s very common for those of us working with Apache Spark or similar data processing libraries.
Of course, you can recreate the above example today just fine with list comprehensions or with the existing filter and map functions. These methods would help more when you have something a bit more complex.
Some obvious reasons not to add methods like these to Python’s iterables:
One obvious way: Though method chaining is a handy way to express complex manipulations on collections, perhaps most Python users would just find it a confusing addition to the existing stand-alone functions and comprehensions.
Mutability: The libraries and languages I’m familiar with that use fluent interfaces usually also default to immutable data structures. Perhaps method chaining is a bad combination with mutable collections, though I confess I haven’t thought much about how that might be.
Anonymous functions: If we add a feature like this to Python, pretty soon after people will be asking for a more flexible lambda so they can express logic that spans multiple lines. I don’t recall the history, but I believe the Python community has rejected this idea in the past.
Has this idea been discussed before? What do people think?
I would be happy to read the prior discussion(s). I searched the forum before posting and didn’t find anything relevant. The suggested topics that show up on the right as you compose a new thread also didn’t show me anything relevant.
One of the biggest reasons is that “iterable” isn’t a class, so you can’t simply add methods to it. It’s a protocol that’s defined by one single method: an __iter__ method that returns an iterator.
If you want ths sort of fluent intreface, one option would be to create a wrapper that takes any iterable and returns an object with all the methods you want. However, each of those methods would have to manage the fluent interface, rather than relying on the individual iterables.It wouldn’t be TOO hard, but it would be something up to you for your particular purposes, rather than being a good fit for the language itself.