Two-Dimensional Expression Layout

Steve D'Aprano steve+python at pearwood.info
Sat Aug 20 21:25:58 EDT 2016


On Sun, 21 Aug 2016 10:43 am, Michael Selik wrote:

> On Sat, Aug 20, 2016 at 6:21 PM Lawrence D’Oliveiro
> <lawrencedo99 at gmail.com> wrote:
> 
>> >     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)
>>
>> That’s an example of what I mean about obscure structure.
>>
> 
> To each his own. I was assuming all those variable names meant something
> to you. If not, then I expect it'd read well with good names. In this
> example, I think the abbreviations and numbers in the names could be
> changed to something more meaningful.
> 
> 
>> >> 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]):
>> >         ...
>>
>> Spot the bug in your version...
>>
> 
> It'd be easier if I ran the code :-)
> Let's see...
> - the ``or`` translates to an ``any(...)``
> - ``not isinstance(obj, Image)`` is repeated 3 times
> - ``[src, mask, dest]`` corresponds to the 3 objects
> - ``mask != None`` is unnecessary, unless somehow None has been registered
> as an instance of Image.
> 
> ... can't spot it. Give me a hint?

Earlier, Lawrence wrote about this same piece of code that using any() or
all() was not a good idea because, and I quote:

"There is no short-cut evaluation when constructing tuples and lists."

I think that he is envisaging a scenario where (say) src and mask are
defined, but dest is not, so 

    [src, mask, dest]

will raise a NameError, but his earlier version of the code:


    if (
            not isinstance(src, Image)
        or
            mask != None and not isinstance(mask, Image)
        or
            not isinstance(dest, Image)
    ) :


will not, *provided* one of the tests on src or mask fail first.





-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.




More information about the Python-list mailing list