What is the output of the following? “`python from functools import partial def power(base, exp): return base ** exp square = partial(power, exp=2) cube = partial(power, exp=3) print(square(4), cube(3)) “`

Python Developer Hard

Python Developer — Hard

What is the output of the following? “`python from functools import partial def power(base, exp): return base ** exp square = partial(power, exp=2) cube = partial(power, exp=3) print(square(4), cube(3)) “`

Key points

  • `partial` from `functools` allows creating new functions with pre-set arguments
  • `square` and `cube` are partial functions with the `exp` argument pre-set to 2 and 3 respectively
  • The `power` function calculates the base raised to the power of the pre-set `exp`

Ready to go further?

Related questions