Two-Dimensional Expression Layout
Michael Selik
michael.selik at gmail.com
Sat Aug 20 08:48:16 EDT 2016
On Fri, Aug 19, 2016 at 5:01 AM Lawrence D’Oliveiro <lawrencedo99 at gmail.com>
wrote:
> It is handy to be able to keep complex expressions together sometimes,
> when breaking them up would simply obscure their structure. To avoid lines
> getting long, why not take advantage of the two available screen/page
> dimensions to make their structure clearer? As a bonus, spacing out
> parentheses makes them look less of a clutter.
>
>
> Examples from <
> https://github.com/ldo/qahirah_examples/blob/master/operators>:
>
> A function call with complex arguments (specifying arguments by keywords
> is highly recommended here):
>
> rect_1_pattern = \
> qah.Pattern.create_linear \
> (
> p0 = (0, 0),
> p1 = (major_dim, 0),
> colour_stops =
> (
> (0, rect_1_colour),
> (1, complement(rect_1_colour)),
> )
> )
>
I'd rather have intermediate variables and avoid the non-semantic
indentation.
p0 = (0, 0)
p1 = (major_dim, 0)
colour_stops = (0, rect_1_colour), (1, complement(rect_1_colour))
rect_1_pattern = qah.Pattern.create_linear(p0, p1, colour_stops)
Computing a variable value (using redundant parentheses to avoid
> backslash-continuations):
>
> dest_rect = \
> (
> draw_bounds
> +
> Vector(col, row) * draw_bounds.dimensions
> +
> Vector(0, top_extra)
> )
>
I'd rather have one long line for this case, even if it's "too long" by PEP
8 standards. If it's too long to think about, I'd break the task into
parts, each with a well-considered variable name.
substep = a + b
final = substep + c
>From <https://github.com/ldo/python_pixman/blob/master/pixman.py>, a
> complex condition (with redundant parentheses again):
>
> if (
> not isinstance(src, Image)
> or
> mask != None and not isinstance(mask, Image)
> or
> not isinstance(dest, Image)
> ) :
> raise TypeError("image args must be Image objects")
> #end if
>
No need for the separate calls to isinstance, nor the check for None.
if any(not isinstance(obj, Image) for obj in [src, mask, dest]):
...
If you prefer maps to comprehensions:
is_image = lambda obj: isinstance(obj, Image)
if any(map(is_image, [src, mask, dest])):
...
More information about the Python-list
mailing list