December 2000
Intermediate to advanced
816 pages
16h 57m
English
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, ...
Read now
Unlock full access