scope hacks 
Author Message
 scope hacks

I like local functions that can be recursive AND have access
to the enclosing function's state.  I tried to use locals()
in the following way and ran into a bug; is it me or python 1.5?

With the tagged line, everything works as expected.  Without
the tagged line, inner's second print is {'j' : <value>};
it doesn't include inner's definition.  (It behaves like a
bogus optimization, namely, not adding local fns to the local
scope unless there's some "not call" reference or an explicit
locals(), the bogosity being that the default for v is an
explicit use of locals().)

def outer(j):
    def inner(i, v = locals()):    # v should get outer's locals()
        print "inner: " + str(i)
        print v
        if i:
            return i + v['inner'](i-1)
        else:
            return 1
    locals()             # why is this line necessary?

    print "outer"
    inner(j)

thanks,
-andy

Sent via Deja.com http://www.*-*-*.com/
Before you buy.



Sun, 08 Dec 2002 03:00:00 GMT  
 
 [ 1 post ] 

 Relevant Pages 

1. hacking,anti-hacking,registry tweaks,compter tricks

2. a cute hack -- delegation after doesNotUnderstand

3. LinkJ fractions hack

4. LinkJ combinations hack for J

5. J hack capturing command line arguments

6. Appearance Hacking for Compliance

7. hacking memory

8. Hacking Clarion for DOS 2.1 Protection!

9. a cute hack -- delegation after doesNotUnderstand

10. a cute hack -- delegation after doesNotUnderstand

11. Linethickness in reports? (HACK solution)

12. Need a Y2K Hack / Fix for '87 Clipper.Lib

 

 
Powered by phpBB® Forum Software