0

Take this example:

presets = [
    "eggs",
    "bacon"
    ]

print(presets[0])
>>> eggs

Why can a not do the same thing, with a list of items to execute? Take this example:

from animations import animation_2, animation_3, animation_4
presets = [
    animation_2.iterate(animations_templates_path, thumbnails_final),
    animation_3.iterate(animations_templates_path, thumbnails_final),
    animation_4.iterate(animations_templates_path, thumbnails_final)
    ]

When I run this (both WITH and WITHOUT the preset[n]) it executes all three commands in the list. Why is this? I would like to have a list of those presets, and call them via am index number. What am I doing wrong?

3
  • 1
    You can do exactly the same thing, if you store the callables and not the result of calling them. Just store e.g. animation_2.iterate, then pass the arguments when you take them back out of the list. Commented Dec 21, 2016 at 17:17
  • Store as presets = [animation_2.interate, ...]. Then call by .............................................. presets[0](animations_templates_path, thumbnails_final) Commented Dec 21, 2016 at 17:20
  • If you saw [factorial(4), factorial(5)] in a program, would you think the first element of that list is a command you can retrieve and execute later, or would you think it's the factorial of 4? Same with your code. Commented Dec 21, 2016 at 17:21

2 Answers 2

2

It executes the items because that's what you are telling it to do. Your code is exactly the same as this:

p1 = animation_2.iterate(animations_templates_path, thumbnails_final)
p2 = animation_3.iterate(animations_templates_path, thumbnails_final)
p3 = animation_4.iterate(animations_templates_path, thumbnails_final)
presets = [p1, p2, p3]

Python has no way of knowing that you didn't intend to call those functions.

One solution is to store a tuple:

presets = [
    (animation_2.iterate, animations_templates_path, thumbnails_final),
    (animation_3.iterate, animations_templates_path, thumbnails_final),
    (animation_4.iterate(animations_templates_path, thumbnails_final),

]

That stores the function and the arguments without calling the function. You can iterate over the list at a later date and execute the function.

Sign up to request clarification or add additional context in comments.

1 Comment

This is extremely helpful in allowing me to understand what I am doing. However, I am going to mark the other answer as the solution because in my use case, it allow me to write less code and iterate over it easier.
1

You can store the actual function objects in a list

from animations import animation_1, animation_2, animation_3
presets = [
    animation_2.iterate,
    animation_3.iterate,
    animation_4.iterate
    ]

Then call the desired function based on its index. This way the function is not executed upon constructing the list, rather it is only executed once you call it.

presets[0](animations_templates_path, thumbnails_final)

1 Comment

This is exactly what I was looking for. Thank you!

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.