Python vs. Lisp: scope issues

A

anton muhin

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.
 
D

David Eppstein

anton muhin said:
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.
 
B

Bengt Richter

anton muhin said:
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
 
A

Alex Martelli

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top