[Python-Dev] Switch statement

Talin talin at acm.org
Fri Jun 16 06:35:58 CEST 2006


Phillip J. Eby wrote:
> As has already been pointed out, this
> 
> 1) adds function call overhead,
> 2) doesn't allow changes to variables in the containing function, and
> 3) even if we had a rebinding operator for free variables, we would have 
> the overhead of creating closures.
> 
> The lambda syntax does nothing to fix any of these problems, and you can 
> already use a mapping of closures if you are so inclined.  However, you'll 
> probably find that the cost of creating the dictionary of closures exceeds 
> the cost of a naive sequential search using if/elif.

This brings me back to my earlier point - I wonder if it would make 
sense for Python to have a non-closure form of lambda - essentially an 
old-fashioned subroutine:

    def foo( x ):
       x = 0
       sub bar: # Arguments are not allowed, since they create a scope
          x = y # Writes over the x defined in 'foo'

       bar()

The idea is that 'bar' would share the same scope as 'foo'. To keep the 
subroutine lightweight (i.e. just a single jump and return instruction 
in the virtual machine) arguments would not be allowed.

-- Talin


More information about the Python-Dev mailing list