Hacker News new | past | comments | ask | show | jobs | submit login

so if I did this (following from your example):

  n = 35
  a = foo(n)
  a()
  n = 42
  a()

I would get 35 printed both times, despite n having changed. is that the idea?



Language specifics mean that this might not always be the case. i.e.

    >>> s = "%03d"
    >>> f = lambda x: s % x # Identical behavior for a regular function closure
    >>> map(f, range(10))
    ['000', '001', '002', '003', '004', '005', '006', '007', '008', '009']
    >>> s = "%04d"
    >>> map(f, range(10))
    ['0000', '0001', '0002', '0003', '0004', '0005', '0006', '0007', '0008', '0009']
I would try and think of it as more of a function parameter that you pre-fill, and never have to provide again for that instance of the function.




Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: