0

Basically, what I want to do is this:

for i in range(10):
  def function[i_value]():
    pass

Which would define these functions: function0, function1, ..., function9.

1

1 Answer 1

1

If these functions are going to be a single-line expressions-only functions you can use lambda:

for name in ['func_1', 'func_2']:
    name = lambda: None

Though I don't see why you would want to do it, as the functions won't be accessible once the for loop is done.

You can store them in a dict though for later access:

funcs = {}
for name in ['func_1', 'func_2']
    funcs[name] = lambda: None

print funcs['func_1'] 

>> <function <lambda> at 0x0019B1E0>
Sign up to request clarification or add additional context in comments.

1 Comment

I actually don't understand the use of lambda, but thanks for the dict idea!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.