In my previous post, I suggested that the status quo:
iter(myobj)
is superior to the suggested method-based syntax:
myobj.iter()
I stand by that. But I will give one exception, and suggest that so long
as we don't have a good syntax for that, this request will never go away
for long:
Function and method chaining.
Procedural/function syntax for chains of function calls suck. It is too
verbose (heavy on parentheses) and written backwards:
print(sort(filter(transform(merge(extract(data)), args))))
To understand it, you have to read forward to find the *last* function
call, which is actually the *first* call, then read backwards.
An alternative that works with mutable data is verbose and expensive in
vertical real estate:
data.extract()
data.merge()
data.transform(args)
data.filter()
data.sort()
data.print()
There is a powerful design pattern to fix this, that works great with
immutable data and functions:
https://martinfowler.com/articles/collection-pipeline/
Shells such as bash have an excellent syntax for this:
data | extract | merge | transform args | filter | sort | print
Method chaining is good too:
data.extract().merge.transform(args).filter().sort().print()
except for the downsides discussed previously. It would be very, very
nice if we had syntactic sugar for that chain of function calls that
would work on general functions and methods.
A long time ago, I wrote a helper class to do that:
https://code.activestate.com/recipes/580625-collection-pipeline-in-python/?in=user-4172944
Heavy data processing frameworks and libraries like Pandas already use
method chaining extensively. It would be great if we could chain
function calls.
--
Steve
_______________________________________________
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/HEERXDNFQE4MESXOYRBOR2VWGDSSLDGA/
Code of Conduct: http://python.org/psf/codeofconduct/