Is there a way to change the closure of a python function?
Steven D'Aprano
steve+comp.lang.python at pearwood.info
Wed Sep 28 05:22:39 EDT 2016
On Wednesday 28 September 2016 15:27, Gregory Ewing wrote:
> * No side effects (new variable bindings may be created, but
> existing ones cannot be changed; no mutable data structures).
As I understand it, for some advanced functional languages like Haskell, that
is only true as far as the interface of the language (the API) is concerned.
Implementation-wise, the language may in fact use a mutable data structure,
provided it is provable that only one piece of code is accessing it at the
relevant times.
The analogy with Python is the string concatenation optimization. Officially,
writing:
a + b + c + d + e
in Python has to create and destory the temporary, intermediate strings:
(a+b)
(a+b+c)
(a+b+c+d)
before the final concatenation is performed. But, provided there is only one
reference to the initial string a, and if certain other conditions related to
memory management also hold, then this can be optimized as an in-place
concatenation without having to create new strings, even though strings are
actually considered immutable and growing them in place is forbidden.
My understanding is that smart functional languages like Haskell do that sort
of thing a lot, which avoids them being painfully slow.
--
Steven
git gets easier once you get the basic idea that branches are homeomorphic
endofunctors mapping submanifolds of a Hilbert space.
More information about the Python-list
mailing list