Python vs. Lisp: scope issues

Discussion in 'Python' started by anton muhin, Oct 14, 2003.

  1. anton muhin

    anton muhin Guest

    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.
     
    anton muhin, Oct 14, 2003
    #1
    1. Advertising

  2. In article <bmh8bf$f79$>,
    anton muhin <> wrote:

    > 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


    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
     
    David Eppstein, Oct 14, 2003
    #2
    1. Advertising

  3. On Tue, 14 Oct 2003 10:59:50 -0700, David Eppstein <> wrote:

    >In article <bmh8bf$f79$>,
    > anton muhin <> wrote:
    >
    >> 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

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

    I don't know if a little class isn't just as good, e.g., (I made it initializable, but that's
    a nit).

    class Accumulator(object):
    __slots__ = ['n']
    def __init__(self, n=0): self.n = n
    def __call__(self, n): self.n +=n; return self.n

    a = Accumulator()


    BTW, can you plug in a __slots__ in the class dict via a metaclass and have it take effect,
    or is that too late? IWT you could, but the class body definition has already been executed
    and its elements bound in a dict, so will that dict be converted after the metaclass mod
    to a slotted version?

    Regards,
    Bengt Richter
     
    Bengt Richter, Oct 16, 2003
    #3
  4. __slots__ from metaclass (was Re: Python vs. Lisp: scope issues)

    Bengt Richter wrote:
    ...
    > class Accumulator(object):
    > __slots__ = ['n']
    > def __init__(self, n=0): self.n = n
    > def __call__(self, n): self.n +=n; return self.n
    >
    > a = Accumulator()
    >
    >
    > BTW, can you plug in a __slots__ in the class dict via a metaclass and
    > have it take effect, or is that too late? IWT you could, but the class
    > body definition has already been executed and its elements bound in a
    > dict, so will that dict be converted after the metaclass mod to a slotted
    > version?


    type.__new__ is what normally applies '__slots__', if found in the
    class dict. So, it is necessary and sufficient for your own metaclass's
    __new__ to insert the '__slots__' you want to have before delegating
    the rest of the operation to type.__new__. My presentation on
    metaclasses at http://www.strakt.com/docs/ep03_meta.pdf (which I just
    found out has the mistaken title "The Template DP in Python", from
    _another_ presentation I gave at the same conference -- ouch) gives
    an example of a custom metaclass injecting __slots__ apropriately.


    Alex
     
    Alex Martelli, Oct 16, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Paul Opal
    Replies:
    12
    Views:
    1,008
    Paul Opal
    Oct 11, 2004
  2. ann
    Replies:
    13
    Views:
    697
    Patricia Shanahan
    Sep 13, 2005
  3. ekzept
    Replies:
    0
    Views:
    396
    ekzept
    Aug 10, 2007
  4. nanothermite911fbibustards
    Replies:
    0
    Views:
    399
    nanothermite911fbibustards
    Jun 16, 2010
  5. nanothermite911fbibustards
    Replies:
    0
    Views:
    344
    nanothermite911fbibustards
    Jun 16, 2010
Loading...

Share This Page