Two-Dimensional Expression Layout

Michael Selik michael.selik at gmail.com
Sun Aug 21 02:48:50 EDT 2016


On Sun, Aug 21, 2016 at 12:31 AM Lawrence D’Oliveiro <lawrencedo99 at gmail.com>
wrote:

> On Sunday, August 21, 2016 at 12:44:21 PM UTC+12, Michael Selik wrote:
> >
> > On Sat, Aug 20, 2016 at 6:21 PM Lawrence D’Oliveiro wrote:
> >
> >>>     if any(not isinstance(obj, Image) for obj in [src, mask, dest]):
> >>>         ...
> >>
> >> Spot the bug in your version...
> >
> > - ``mask != None`` is unnecessary, unless somehow None has been
> registered
> > as an instance of Image.
>
> That’s the bug: it’s not unnecessary.
>
> Maybe a quick application of one of De Morgan’s theorems might help:
>
>     if (
>             isinstance(src, Image)
>         and
>             (mask == None or isinstance(mask, Image))
>         and
>             isinstance(dest, Image)
>     ) :
>         don’t raise TypeError("image args must be Image objects")
>     #end if
>
> Is that better for you?
>

Indeed it is, not sure why. I think Steven's right in this case. Since the
condition isn't identical for all objects, and it's only three variables,
I'd rather break it apart.

    if not isinstance(src, Image):
        raise TypeError('src must be an Image')
    if mask and not isinstance(mask, Image):
        raise TypeError('mask must be an Image or None')
    if not isinstance(dest, Image):
        raise TypeError('dest must be an Image')

As he said as well, this has the added benefit of encouraging more explicit
error messages.

In programming and in prose, sometimes repetition feels ugly and redundant,
but sometimes repetition feels like a beautiful harmony.



More information about the Python-list mailing list