__str__ vs. __repr__
bjorn at roguewave.com
Sat Nov 13 23:59:34 CET 1999
> M.-A. Lemburg <mal at lemburg.com> wrote:
> > > You may be wondering how this works. The answer is that
> Viperi does not
> > > use bytecode, it executes the 'Abstract Syntax Tree' directly.
> > > Consequently, the original code can be recovered.
> (without comments,
> > > or the original formatting, of course).
> > Could you point me to some resources ? Using ASTs for execution
> > is an interesting subject and I would like to know how you deal
> > with Python dynamic nature in this context (are the ASTs self
> > modifying ?).
> guess my brain doesn't work well today,
> but maybe someone could tell me:
> 1) what's the reason AST's would have to change during
> the execution of dynamic code (the byte code doesn't
> exactly change, does it?)
It doesn't have to. All you have to do is implement an "Environment" object
that maps variable names to objects in the current scope. E.g.:
def eval_assignment(lhs, rhs, env):
left = evaluate(lhs)
right = evaluate(rhs)
env[left] = right
Most simple interpreters work this way (unless you've spent a lot of time on
implementing your bytecode interpreter, it is generally a very modest win).
> 2) what's the reason byte code cannot be used to recover
> the original source code (Swedish readers may remember
> the ABC80, which did exactly this).
As long as the bytecode is relatively close to the language, this is no
problem. (You can even do this for Java bytecodes..)
More information about the Python-list