Python vs. Lisp: scope issues

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • anton muhin

    Python vs. Lisp: scope issues

    In addition to s[0] hack, I'd like to suggest the following code. Any
    comments are highly appreciated:

    class Scope(object):
    pass

    def accumulator():
    scope = Scope()
    scope.n = 0
    def f(n):
    scope.n += n
    return scope.n
    return f

    a1 = accumulator()
    print a1(1)
    print a1(2)

    a2 = accumulator()
    print a2(10)
    print a2(20)

    regards,
    anton.

  • David Eppstein

    #2
    Re: Python vs. Lisp: scope issues

    In article <bmh8bf$f79$1@n ews.peterlink.r u>,
    anton muhin <antonmuhin.REM OVE.ME.FOR.REAL .MAIL@rambler.r u> wrote:
    [color=blue]
    > In addition to s[0] hack, I'd like to suggest the following code. Any
    > comments are highly appreciated:
    >
    > class Scope(object):
    > pass
    >
    > def accumulator():
    > scope = Scope()
    > scope.n = 0
    > def f(n):
    > scope.n += n
    > return scope.n
    > return f[/color]

    Definitely cleaner than the s[0] hack. I'll have to remember that the
    next time I need to work around the scope limitation.

    --
    David Eppstein http://www.ics.uci.edu/~eppstein/
    Univ. of California, Irvine, School of Information & Computer Science

    Comment

    Working...