Some kinds of objects often receive a whole lot of method calls in sequence. In 
these situations, it is handy if each method call ends with “return self”, so 
that you can chain the calls together. This is particularly common with 
graphics APIs, for instance.

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()
    )

Another example 
<http://default-cube.deviantart.com/art/Pattern-Dash-576369003>, where an 
object requires setup calls that cannot all be expressed in the constructor:

    pattern = \
        qah.Pattern.create_linear \
          (
            p0 = (0, 0),
            p1 = (pattern_length, 0), # base orientation is parallel to X-axis
            colour_stops =
                (
                    (0, Colour.from_hsva((0.475, 0.9, 0.8))),
                    (1, Colour.from_hsva((0.975, 0.9, 0.8))),
                )
          ).set_extend(CAIRO.EXTEND_REPEAT)

In <http://default-cube.deviantart.com/art/Shadow-Mask-533143786>, a temporary 
drawing context is created, used to create the image pattern, and then 
discarded, without even having to give it a name:

    mask = qah.ImageSurface.create \
      (
        format = CAIRO.FORMAT_RGB24,
        dimensions = dimensions * scale
      )
    (qah.Context.create(mask)
        .set_matrix(Matrix.scale(scale))
        .set_source_colour(primary[component])
        .set_operator(CAIRO.OPERATOR_SOURCE)
        .rectangle(spot)
        .fill()
    )
-- 
https://mail.python.org/mailman/listinfo/python-list

Reply via email to