0

I want to create a list of squares of even numbers by creating a list, applying the callback functions to each element of the list and puts the result in a list. But how do I implement callback in the first place?

2
  • A callback is a function (technically, any callable, including methods, classes and instances of callable classes, not just functions). Aside from that, you'll need to be more specific. What are you using that needs one? Commented Dec 13, 2017 at 4:11
  • 1
    callback mostly means function name without (). You can use it as argument in other function and it will execute it later using (). So every function or method can be callback. For example in tkinter you can assign function name to button and button will execute this function later, when you click this button. So assigned function is called "callback". Commented Dec 13, 2017 at 4:21

1 Answer 1

1

In this case I think you are looking for the map builtin. It takes a function and a list and then applies that function on the list storing each result as it goes. For example:

 def square(x):
     return x * x

 list(map(square, [1, 2, 3, 4]))
 >>> [1, 4, 9, 16]

Note that we need to cast the result of map back to a list since it returns a map object.

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

4 Comments

Yes, but how does this implement callback?
@RohitMenon: map's first argument is a callback function which it applies to each element of the input, with the output being the result of executing said callback.
This is not a callback, just a function use very similar to one.
@KlausD. Callbacks

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.