I just have a random question about lists in Python: can you call or define functions inside of lists like you can in JavaScript?
An example of this in JavaScript is:
const arrOfFunctions = [...Array(5).keys()].map(i => console.log(`This is hi number ${i}!`));
Each element is an anonymous function (in this example) that prints a message to the console that contains the iteration. The original array is an array of numbers from 0 to 4.
Sorry for not being more specific. I guess I could have returned something in the map I wrote above and it was a poor example.
I was primarily thinking of the case of Promise.all in JavaScript where you want to do a bunch of asynchronous tasks that might use similar functions that themselves return Promises. You can do something if there’s either a successful or unsuccessful outcome of all of the resolved/rejected Promises. You provide an array of the function calls and then can do stuff depending on the outcome of all of the Promises returned by these functions.
I will refrain from showing a code example for the Promise.all example.
However, here is a simpler example:
function addOne(num) {
return num + 1;
}
const arr = [addOne(0), addOne(2), addOne(99)];
console.log(arr);
>>> [1, 3, 100]
You can call your functions directly from inside the array even though what is ultimately stored are numbers in the example above. That’s my question: can you call functions from inside an array in Python?
an array of function calls is not a thing, this does not make sense
you can either supply an array of functions, or an array of results from having called functions, both of which is the same thing, an array of values, or simply an array
Thank you. That’s the kind of answer I was looking for. In JS they’re treated like values. Since JS is what I know best and I’m here to learn Python, that’s why I asked the question in reference to JS.
Can I do the Python equivalent of calling functions inside the array? You seemed to imply that one can’t.
Btw, you can totally put a function reference inside an array in JS. It works and doesn’t throw any error If you do a console log on an array containing function references, it prints the functions.
It doesn’t make sense. You are not doing this in javascript either. You’re mixing up nouns and verbs.
What you are putting in your arrays in javascript is the result of calling your function, not the function.
So what is the result of those functions? Presumably you are talking about specific functions that return promises. So you do not have an array of functions, you have an array of promises.
Perhaps you then create another promise that resolves once any of the promises in the array resolve, or perhaps all, and then you can wait on a single promise instead of writing code to wait for each one.
// JS
const arrOfMyFunction = [myFunction()];
console.log(arrOfMyFunction);
>>> prints whatever results from code of myFunction wrapped inside an array
# Python
list_of_my_function = [my_function()]
print(list_of_my_function)
>>> prints whatever results from code of my_function wrapped inside a list
An array or list of function references or pointers if you prefer… in JS they’re known as references:
// JS
const arrOfReferences = [myFunction];
console.log(arrOfReferences);
>>> prints [[Function: myFunction]]
# Python
arr_of_references = [my_function]
>>> prints [<function my_function at 0x7fe96c274200>]
In the above I just put a function directly inside the array. It returns [[Function: sayHi]]. It looks like I can’t do the same in Python but putting in a pointer to the function works.
My original question was about whether we can have a list of function calls in Python. Some languages do not allow you to do that.
Anyway, I answered my own question and maybe this will be helpful to someone else wondering the same thing. Who knows?
Similarly you can define f and use the variable instead:
def f():
print("Hi!")
print([f])
Like I said, a function is a value, and a list can refer to values, therefore you can put functions in lists.
But you were not putting functions in your list, so you saw no function in your list.
You put True in your list. And javascript behaves exactly the same way.
Which doesn’t put a function in the list. That calls the function, and puts the result of the function in a list. The function has already happened. You called it.
Similarly, if you write this:
[1 + 1]
The value that you are putting in that list is not lambda: 1+1, it is 2
Uh oh, you’ve created a bunch more rabbit holes for me to go down
If I’m understanding you, it looks like Python:
has no pointers
has IIFE’s
has async/await
I’m still on the list classes in the Python 3 course. Knowing about async functions will be helpful since I will be starting a new job that requires Flask.
Thank you very much for your time. I learned a lot!
nothing stopping you from calling a function without naming it, this isn’t a language feature, it’s just an idiom
What you probably mean is functions as expressions, aka lambda aka anonymous function.
But, this is not significant, it’s cosmetic.
yes, but this is not the default execution model, unlike nodejs