[Python-ideas] Method chaining notation
Chris Angelico
rosuav at gmail.com
Sat Feb 22 06:37:13 CET 2014
On Sat, Feb 22, 2014 at 4:16 PM, Steven D'Aprano <steve at pearwood.info> wrote:
> It's not that long: chained() only adds nine characters, and
> if you're worried about that, call it chain() instead (seven). With your
> syntax, every dot lookup takes two chars instead of one, so it only
> takes eight method calls before my syntax is shorter than yours.
>
> # unrealistically short method names just so the example fits on one line
> obj->f()->g()->h()->i()->j()->k()->l()->m()
> chain(obj).f().g().h().i().j().k().l().m()
>
> In practice, I would expect that you would want to split the chain
> across multiple lines, just for readability, and if you're approaching a
> chain ten methods long, your code probably needs a rethink.
Oh, I see. I thought I'd need to call chain() at each step along the
way, in which case it really would be too long.
> Not so -- if the alternative is:
>
> obj.f()
> obj.g()
> obj.h()
> obj.i()
> obj.j()
> obj.k()
> obj.l()
> obj.m()
>
> my version with chain() or chained() or even enable_chained_methods()
> wins hands down.
It certainly beats that version, yeah!
ChrisA
More information about the Python-ideas
mailing list