CRAZY: First class namespaces
Amit Patel
amitp at Xenon.Stanford.EDU
Tue May 30 18:21:08 EDT 2000
Darrell Gallion <darrell at dorb.com> wrote:
|
| If I now understand you're saying, the function captures a scope from where
| it's defined. Or is given a scope on the fly. So could a function instance
| act like a class to produce new functions, each with it's own dynamic scope?
| Maybe a default var could be twisted to this use.
|
| def dyn( self=None):
| self.x=1
|
| def helper(func):
| func()
|
| def test():
| x=0
| # use dyn like a class
| myFunc=dyn(self=locals())
| helper(myFunc)
| assert(x==1)
Yeah, a function can create new functions by using scoping. Here's
the canonical example from Scheme / ML:
def make_adder(n):
def adder(m):
return n+m
return adder
add5 = make_adder(5)
add9 = make_adder(9)
So now add5(3) will be 8, and add9(10) will be 19. You can do the
same with default vars now, by having adder(m, n=n).
- Amit
--
--
Amit J Patel, Computer Science Department, Stanford University
http://www-cs-students.stanford.edu/~amitp/
More information about the Python-list
mailing list