What does Python fix?
François Pinard
pinard at iro.umontreal.ca
Sun Jan 20 15:47:15 EST 2002
[Alex Martelli]
> In Python 2.1, you could also get such lexically nested scopes, but you
> needed a from __future__ import nested_scopes to enable that behavior.
> [...] with lexical scopes and generators there are now a lot of
> alternatives in various situations.
Thanks for the enlightening.
> A class remains the typical and most general way to carry state, and we
> always had other ways, e.g. the default-arguments of a function. [...]
> the first nested def fails since undo_function is yet unbound then. To
> avoid a class, it would take a wee little bit of trickiness, e.g.:
> def modify(redo, undo, focuses):
> temp=[None]
> def redo_function(undo_function=temp):
> redo()
> return undo_function[0], focuses
> def undo_function(redo_function=redo_function):
> undo()
> return redo_function, focuses
> temp[0] = undo_function
> or the like.
I thought and rejected the idea of default-arguments of functions, as
the trick above did not come to my mind. Could be useful for me to to
keep it somewhere in the back of my memory :-). I'll try to find some
time to study the PEP (or Python documentation if any) on nested scopes:
it might be especially worth if the magically implement usable closures.
Thanks for this discussion.
> >> redo_fn, focus_list[:] = undo_list.pop()()
> > 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.
> It's reasonably efficient -- I'm not sure what your worry is. [...]
> generally speaking, Python does no implicit copying: if you need a
> "snapshot" of how a list looks at a given time you copy it explicitly.
All such things, in fact. If the focus list becomes big, it might imply
some extra-time for copying, and some memory for keeping extra-copies.
In Lisp, there is the capability of having genuine pointers all along a
single list. There is probably no memory-efficient equivalent in Python.
Nothing very important for the specific application that yielded the example
under consideration, yet such thinking may have other long-term benefits.
Thanks to all who shared ideas and opinions on this.
--
François Pinard http://www.iro.umontreal.ca/~pinard
More information about the Python-list
mailing list