*apply()

The first built-in function we are looking at is apply(). The apply() function is the most basic of the four and is simply used to pass in a function object along with any parameters. apply() will then invoke that function with the given arguments. There is no special magic here; apply() works exactly the way you think it does, so the following pair of calls are practically identical:

foo(3, 'pyramid')     ⇔      apply(foo, (3, 'pyramid'))

Alternatively, the arguments can be stored in a tuple, and then the function can be called with apply():

args = (4, 'eve', 79)
apply(foo, args)

Note this is not the same as foo(args) which is calling foo() with a single argument (a tuple). Rather, using apply() means calling foo() with three arguments, ...

Get Core Python Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.