questions about functions inside a function

Jeremy Sanders jeremy+complangpython at jeremysanders.net
Mon Jul 16 11:14:11 EDT 2007


fdu.xiaojf at gmail.com wrote:

> What I want is, the value of i should be bounded to the anonymous
> function. And the output should like this:
...
> How to achieve this?

This doesn't answer your question (others have), but another (perhaps
clearer) way to do such things is something like

class MyFunc(object):
  """A function object."""
  def __init__(self, val):
     self.val = val

  def __call__(self):
     """Return value squared"""
     return self.val**2

a = []
for i in range(4):
  a.append(MyFunc(i))

for f in a:
  f()


Jeremy

-- 
Jeremy Sanders
http://www.jeremysanders.net/



More information about the Python-list mailing list