[Python-ideas] Dart-like method cascading operator in Python
Chris Angelico
rosuav at gmail.com
Thu Nov 21 16:00:40 CET 2013
On Fri, Nov 22, 2013 at 1:56 AM, Masklinn <masklinn at masklinn.net> wrote:
> 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).
I get that. But what I'm saying is that this is needing some clear
definitions in terms of indentation and beginnings of lines anyway, so
it clearly cannot conflict with method chaining. It can simply use the
dot, so it'll look like normal method invocation, but with an indent
meaning "same as the previous" - like how a BIND file is often laid
out:
@ IN SOA .... blah blah ....
IN NS ns1.blah.blah
IN NS ns2.blah.blah
IN MX 10 mail
This would have things look pretty much the same:
foo.bar():
.quux()
.asdf()
.qwer()
ChrisA
More information about the Python-ideas
mailing list