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

Nick Coghlan ncoghlan at gmail.com
Mon Jun 13 06:52:45 CEST 2011


On Mon, Jun 13, 2011 at 8:30 AM, Greg Ewing <greg.ewing at canterbury.ac.nz> wrote:
> I'm -1 on any proposal that somehow tries to make the
> default-argument hack more acceptable.
>
> The main reason people still feel the need to use it
> is that the for-loop is broken, insofar as it doesn't
> create a new binding for each iteration.
>
> The right way to address that is to fix the for-loop,
> IMO.

Yikes, now *there's* a radical proposal.

-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

or:

def f():
  return [lambda: i for i in range(2)]

or:

def _inner():
  for i in range(2):
    def g():
      return i
    yield g

def f():
  return list(_inner())

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia



More information about the Python-ideas mailing list