Best search algorithm to find condition within a range

Chris Angelico rosuav at gmail.com
Thu Apr 9 19:56:27 CEST 2015


On Fri, Apr 10, 2015 at 3:35 AM, Steven D'Aprano
<steve+comp.lang.python at pearwood.info> wrote:
> It's not so much the undefined behaviour part that gets me. That's bad
> enough. But the concept that having the compiler ignore what you write in
> the source code because you've accidentally hit some undefined part of the
> spec *is a feature* rather than a horrible, horrible design flaw that blows
> my mind.

In Python, you can create an assert with a side effect. This is
something you should never do, and the compiler is free to optimize
that side effect out. Is that outright malicious? The only difference
is that in Python, it's well-defined - you can *depend* on that code
being optimized out under certain specific conditions, rather than it
depending on the compiler. (At least, I think that's the case. Is it
legal for a Python implementation to fully evaluate asserts and then
simply not raise the exception?)

Take this example:

def spam(lst):
    assert lst[0] < lst[1]
    # carry on with the rest of the code

If lst is a Python list, this is side-effect free. But what if it
isn't? What if __getitem__ actually mutates the object? The assertion
can be dropped, and the getitem calls not made. Is that a horrible,
horrible design flaw in Python, or is it an ill-designed object that
does things that programmers should be allowed to assume never happen?

ChrisA



More information about the Python-list mailing list