Method Chaining
Ned Batchelder
ned at nedbatchelder.com
Fri Jun 17 04:13:23 EDT 2016
On Friday, June 17, 2016 at 12:37:14 AM UTC-4, Lawrence D’Oliveiro wrote:
> On Friday, June 17, 2016 at 4:24:24 PM UTC+12, Michael Selik wrote:
> > On Thu, Jun 16, 2016 at 10:53 PM Lawrence D’Oliveiro wrote:
> >
> > > Example from <http://default-cube.deviantart.com/art/Truchet-612095093>,
> > > concisely expressing a complex drawing sequence:
> > >
> > > (g
> > > .move_to((p1 + p2a) / 2)
> > > .line_to(p1 + (p2 - p1) * frac)
> > > .line_to((p1 + p1a) / 2)
> > > .stroke()
> > > .move_to((p2 + p2a) / 2)
> > > .line_to(p2 + (p1 - p2) * frac)
> > > .line_to((p2 + p1a) / 2)
> > > .stroke()
> > > )
> >
> > Wouldn't that look nicer with the ``g`` repeated on every line, no extra
> > indentation, and no extraneous parentheses?
> >
> > g.move_to((p1 + p2a) / 2)
> > g.line_to(p1 + (p2 - p1) * frac)
> > g.line_to((p1 + p1a) / 2)
> > g.stroke()
> > g.move_to((p2 + p2a) / 2)
> > g.line_to(p2 + (p1 - p2) * frac)
> > g.line_to((p2 + p1a) / 2)
> > g.stroke()
>
> Clearly, no.
To me, it's a toss-up. The chained version is nice in that it removes the
repetition of "g". But the unchained version is more explicit, and avoids
the awkward parenthesis.
I think I would lean toward the unchained version. Clearly tastes can
differ.
--Ned.
More information about the Python-list
mailing list