Space Python Loading Python…

Default Parameter Values

 
0:00 / 0:00

Challenge

Practise what this example shows with these challenges.

Sensible defaults

Write the codeEasy 2 stages

Give send_command(cmd, retries=3, timeout=5.0) default values so callers only pass what differs, call it three different ways, then add a new priority parameter without breaking any existing call.

Start the challenge

Shared default list

Fix the codeMedium 3 stages

add_sample(sample, log=[]) remembers samples from earlier calls, because the default list is created once and shared. Predict it, prove the calls share one list, fix it with None as the default, then write a function that hands out a fresh list each time.

Start the challenge

Prove the fix

Write the codeMedium 2 stages

Write assert checks for the fixed add_sample() that would have caught the shared default list, then add checks that a log passed in is still the one that gets the sample, so a "fix" that breaks that can't pass either.

Start the challenge

Discussion

Stuck on this example or want to go further? Ask here, or see what others asked.

Log in to ask and reply.

script.py
PythonLn 1, Col 1Spaces: 4
Loading the Python environment…

Separate arguments with commas. They're passed on every Run as sys.argv[1:].