Method Chaining

Michael Selik michael.selik at gmail.com
Fri Jun 17 00:23:57 EDT 2016


On Thu, Jun 16, 2016 at 10:53 PM Lawrence D’Oliveiro <lawrencedo99 at gmail.com>
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()

Sometimes it's hard to know when a function has a side-effect. I appreciate
that these impure functions tend to return None.



More information about the Python-list mailing list