Modifying func_closure
Christopher T King
squirrel at WPI.EDU
Fri Jul 9 10:14:32 EDT 2004
On 9 Jul 2004, Jacek Generowicz wrote:
> I'm trying to do something like the following. (I'm no Schemer, so
> there may well be a more concise way of putting it.)
>
> (define closures
> (let ((enclosed 1))
> (define (report) enclosed)
> (define (modify new) (set! enclosed new))
> (list report modify)))
>
> (define report (car closures))
> (define modify (cadr closures))
>
> (report) ; -> 1
> (modify 2)
> (report) ; -> 2
>
> The mechanism isn't important, it's the effect: "enclosed" is rebound;
> it's value changes in a way which all enclosing closures notice.
Is there a reason you couldn't encapsule the functions and 'enclosed' in a
class (assuming you can move the function definitions into a class)?
More information about the Python-list
mailing list