Skip to content
Foxipa
Code Completion 5 Challenges

Complete the partial-function and method code

Practice pre-filling callable arguments and defining methods with functools.partialmethod while preserving instance binding.

Intermediate ~8 min read

Challenge 1

Create a callable that stores the first argument so each later call only needs a repetition count.

python Your answer
from functools import partial

def repeat(text, count):
    return text * count

repeat_warning = 
print(repeat_warning(2))

Press Ctrl⌘Enter to check your answer

Challenge 2

Build a callable that parses binary text without requiring callers to pass the base each time.

python Your answer
from functools import partial

def parse_integer(value, *, base):
    return int(value, base=base)

parse_binary = 
print(parse_binary("1011"))

Press Ctrl⌘Enter to check your answer

Challenge 3

Override the stored prefix for this one call.

python Your answer
from functools import partial

def label(text, *, prefix):
    return f"{prefix}{text}"

draft = partial(label, prefix="Draft: ")
print(draft("Report", prefix=))

Press Ctrl⌘Enter to check your answer

Challenge 4

Configure round() to round each value to one decimal place, then use it as map() callback.

python Your answer
from functools import partial

values = [1.234, 2.678, -3.141]
round_one_decimal = 
print(list(map(round_one_decimal, values)))

Press Ctrl⌘Enter to check your answer

Challenge 5

Define as_upper_csv as a method variant that pre-fills the extension with "csv" and requests uppercase titles. Let Python bind each Report instance normally.

python Your answer
from functools import partialmethod

class Report:
    def __init__(self, title):
        self.title = title

    def export(self, extension, *, uppercase=False):
        name = self.title.upper() if uppercase else self.title
        return f"{name}.{extension}"

    as_upper_csv = 

print(Report("monthly report").as_upper_csv())

Press Ctrl⌘Enter to check your answer