What are python closures realy like?

Fredrik Lundh fredrik at pythonware.com
Wed Dec 6 06:31:25 EST 2006


Paul Boddie wrote:

> I know that everyone will say that Python is a "multi-paradigm"
> language and that one should feel free to use whatever technique seems
> appropriate to solve the problem at hand, but it seems to me that
> there's been an explosion in nested function usage recently, with lots
> of code snippets showing them off either in the context of a debugging
> exercise or as a proposed solution to a problem, and yet in many cases
> their usage seems frivolous in comparison to plain old object-oriented
> techniques.

when doing some heavy optimization, I recently found myself writing:

    def foobar(arg1, arg2, arg3):
        def helper(arg):
             do something with arg1 and argument
        def foo():
             do something with arg1 and arg3 and
             call helper
        def bar():
             do something with arg1 and arg2
        def zoo():
             do something with arg2 and arg3 and
             call helper
       # oops; how do I return all these?
        class bag(object):
             pass
        bag = bag()
        bag.foo = foo
        bag.bar = bar
        bag.zoo = zoo
        return bag

which, I think, deserves no further comment...

</F> 






More information about the Python-list mailing list