I *think* there is a way of getting at the stack trace from within a
function. There certainly is a way to do this from within an
exception handler -- the Python debugger uses this.
>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.
Well, there's "raise" for that, which is never 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.
Now there's an interesting idea. It wouldn't be too hard to break
parts of the interpreter open and allow the user to specify "hooks"
that are called when various interesting things happen, such as
calling a function, returning from a function, or perhaps executing
any instruction. This could easily be specified on a per-function
basis, and would make for a very general tracing and debugging
I'd like to define it in such a way that it can't be used (or at least
doesn't encourage use) to change the "local" semantics of Python, but
side effects of the kind that a trace or log facility must have would
--Guido van Rossum, CWI, Amsterdam <firstname.lastname@example.org>
"All right, it's a fair cop, but society is to blame."