Give me a better one then.

I need a way for quickly annotating loops with invariants. The problem with context managers is that it needs indentation change which is definitely not "quick".

Imagine I wanted to take code from some public repo and add loop invariants to it. If the diff touches half of the lines of the code, it will never be merged.

We can quickly annotate functions with decorators and type hints (both are one-liners).

We can quickly specify assertions (one-liner).

Now give me one-liner for loop invariants.


On 11/04/20 10:20 am, haael wrote:

     class InvariantError(AssertionError):
         pass

     def loop_invariant(condition:bool):
         if not condition:
             raise InvariantError
         return True

     for element in iterable if loop_invariant(condition):
         loop_body

That's a *very* misleading way to check loop invariants,
because it makes it look like a filter rather than an
invariant check.

Also it doesn't quite do a complete job, because it
doesn't check the invariant after the last iteration,
and doesn't check it at all if the iterable is empty.

     while loop_invariant(condition) and running:
         loop_body

Again misleading, because it looks like part of the
loop condition.

_______________________________________________
Python-ideas mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/[email protected]/message/C6LILBZ4GSJVQXZQBWXNXUNYTY4AT7II/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to