avatar Bite 172. Having fun with Python Partials

Meet another gem in the standard library: functools, which contains tools for functional-style programming.

In this Bite you will play with its useful partial() function which - as per PEP 309 - lets you construct variants of existing functions that have some of the parameters filled in.

The original PEP (Python 2) also shows a small but realistic example:

import functools

def log (message, subsystem):
    "Write the contents of 'message' to the specified subsystem."
    print '%s: %s' % (subsystem, message)
    ...

server_log = functools.partial(log, subsystem='server')
server_log('Unable to open socket')

In this Bite you will apply this concept to the round builtin:

>>> round(10.42342, 2)
10.42

Write two partials to add the default behavior of rounding to 0 and 4 places respectively.

See the template code below. If you get stuck maybe this article will help you.

Good luck and keep calm and code in Python!

Login and get coding
go back Beginner level
Bitecoin 2X

320 out of 322 users completed this Bite.
Will you be the 321st person to crack this Bite?
Resolution time: ~22 min. (avg. submissions of 5-240 min.)
Our community rates this Bite 2.09 on a 1-10 difficulty scale.
» You can do it! 😌

Focus on this Bite hiding sidebars, turn on Focus Mode.

Ask for Help