What does Python fix?

François Pinard pinard at iro.umontreal.ca
Sat Jan 19 12:20:06 EST 2002


[Jason Orendorff]

> I've tried to translate the Scheme into roughly equivalent Python,
> to see if it's really longer.  Take a look.

Thanks for the exercise, Jason.

> def modify(redo, undo, focuses):
>     def redo_function():
>         redo()
>         return undo_function, focuses
>     def undo_function():
>         undo()
>         return redo_function, focuses

This might not work, I'm not sure.  Take `undo_function', in the `return'
statement within `redo_function': it is neither global nor local, so I
guess it might not be defined.  Don't we need to resort to a Python class,
for correctly representing a Scheme closure containing functions meant to
survive outside the scope of their definition?

I did not study what happened to scopes since Python 1.5.2, I know that
there are changes in this area, so maybe the problem I see is already gone.

> def undo():
>     if len(undo_list) == 0:
>         warning(_("No more undo!"))
>     redo_fn, focus_list[:] = undo_list.pop()()
>     redo_list.append(redo_fn)

I'm only worried a bit by the replacement of the whole focus_list, not
knowing how efficient such an operation is.  But even if it was slow,
it would be bearable in this case.

> >     (define (replace value focuses)

> I admit this stumped me.  :-) Perhaps with some more context I could
> have figured it out.

I just took this from a bigger example, because Huaiyu Zhu asked for an
example and I did not feel like building one for demonstration.

> def replace(value, focuses):
>     restore = focuses[0]
>     if len(focuses) == 1:
>         def redo():  focuses[0] = value
>         def undo():  focuses[0] = restore
>         modify(redo, undo, focuses)

I did not study all the implications in detail, but yet, wonder if the `redo'
and `undo' function correctly photograph the current value of `focuses', at
the time of the definition occurred, for later use.  I would be tempted to
doubt it.  Especially given that slice replacement is used in other places.

I still have the overall feeling that, without special care, it is not that
easy to write Python code that correctly represents the original Scheme.
It might require more vertical space, including a few more accessory classes.

-- 
François Pinard   http://www.iro.umontreal.ca/~pinard




More information about the Python-list mailing list