[Python-ideas] 'Injecting' objects as function-local constants

Greg Ewing greg.ewing at canterbury.ac.nz
Tue Jun 14 00:37:20 CEST 2011


Nick Coghlan wrote:

> -lots on any idea that would make:
> 
> def f():
>   i = 0
>   def g1():
>     return i
>   i = 1
>   def g2():
>     return i
>   return [g1, g2]
> 
> differ in external behaviour from:
> 
> def f():
>   result = []
>   for i in range(2):
>     def g():
>       return i
>     result.append(g)
>   return result

One possible variation of my idea wouldn't change the existing
behaviour of the for-loop at all, but would require you to
explicitly request new-binding behaviour, using something like

    for new i in range(2):
      def g():
        return i

-- 
Greg



More information about the Python-ideas mailing list