Functions are values
Not met yet
A function is a value, like a number or a list. Its name without brackets is the function itself: you can give it another name, keep it in a list or a dictionary, or pass it to another function. The brackets are what call it. sorted(key=...), map, filter and decorators all rely on this.
Try it
main.pydef shout(text):
return text.upper()
announce = shout
print(announce("fresh figs"))
calls = {"shout": shout, "plain": str.lower}
print(calls["plain"]("FRESH FIGS"))
Output
Press Run and what your program prints shows up here.
Easy to get wrong
- Thinks passing a function by name calls it there and then, so announce(shout, ...) runs shout with no arguments.
- Thinks a function can't be stored in a variable, kept in a list or dictionary, or passed to another function.