2

What is the way to "burn in" an external variable to a function upon definition?

funcs = []
for n in range(5):
    def f(x):
        return n * x
    funcs.append(f)

del n

But trying

[g(2) for g in funcs]

or

funcs[1](2)

Gives an error

NameError: name 'n' is not defined

Is there a way to allow defining a function with a parameter which will become fixed for the function scope?

Aguy
  • 7,851
  • 5
  • 31
  • 58

0 Answers0