Re: Other Python syntax changes

Daniel LaLiberte (liberte@ncsa.uiuc.edu)
Tue, 10 Dec 91 12:00:47 CST

Assertions can easily be done without changes to the language, e.g. by
writing a function like this:

def assert(test):
if not test: raise <some exception>

Although using a simple assert function goes a long way, there is more
that an full-blown assertion mechanism should do for the programmer.
e.g. often one wants to compare the previous value of an expression (when
a function was entered) with the current value. Eiffel has a pretty
good assertion mechanism melded in with the class system, to constrain
the relationships between super classes and subclasses. I dont think
you would want to go that far, but it has some good ideas.

The exception that is raised by a failed assertion should reflect the
caller of the assertion somehow, as well as the type of exception being
raised. If the assert function can get at the caller by itself, then
all we need is a second parameter to indicate the exception type.

More daring folks (like myself) will want to compile out assertions
when they are satisfied that the code is "safe". But assertions could
still be used as a control construct (when the exception is caught), so not
all assertions should be compiled out.

How could one do module or class invarients in Python? These are
assertions that hold after any function in the module or class is
called. One would want to declare the assertion only once and have
it called automatically after each function call (or whenever the
state of the module or class instance may be changed). If there were a
hook on function calling or message sending, then a module or class
could just call the assertion in the hook function for that module or class.

An automatic type checker should be able to use assertions to assist
in type checking.

To do comparison with old values in current Python, one would have to
save the old value in a local variable explicitly, which is probably
more flexible and not too difficult. Eiffel also has a notion of a
loop varient which is a non-negative integer expression that always
decreases in value after each loop. This would be pretty messy to code
explicitly and probably wouldnt be used much in Python since many loops
are implicit or are already guarenteed to halt.

Well, enough for now.
dan