[Python-ideas] Dart-like method cascading operator in Python
Masklinn
masklinn at masklinn.net
Thu Nov 21 15:56:06 CET 2013
On 2013-11-21, at 15:19 , Chris Angelico <rosuav at gmail.com> wrote:
> On Fri, Nov 22, 2013 at 1:08 AM, Perešíni Peter <ppershing at gmail.com> wrote:
>> Agree, this example is extra bad and unreadable. If we want cascading (and
>> especially nested cascading), I would force cascading operator to be the
>> first token on a new (and indented) line as in my examples
>
> In that case, why have a separate cascade operator?
>
> o = MyObject()
> .set(x)
> .set(y)
The primary issue with it is that the API must be crafted specifically
for chaining: everything must be done with methods, and methods must
return their `self`. Not only does this mean mutator methods which could
return something else can’t, it goes against the usual Python grain
(at least that of the builtins and standard library) where mutator
methods generally return None.
Cascading adds “chaining” to all (mutable) objects without having to
alter them or build the API specifically to that end. Or duplicate
setattr & setitem via additional methods.
But yes, as noted if the situation of “infix operators line breaks” is
not changed, it will also affect chaining (not that I think it’s a big
deal to put a chain in parens).
More information about the Python-ideas
mailing list