← Quiz: Python Functions: all 15 questions
Quiz: Python Functions · Question 14 of 15
What is the safe way to give a function an optional list parameter?
What is the safe way to give a function an optional list parameter?
- A.def f(items=[]):
- B.def f(items=None): and create a new list inside when items is None
- C.def f(items=list):
- D.def f(items=()): and append to it
Answer & explanation
Correct answer: def f(items=None): and create a new list inside when items is None
A default value is created once, so a [] default is shared between calls. None plus a new list inside avoids that.
Can you get the rest right without peeking?
Take the timed Quiz: Python Functions quiz free. Pass it to earn XP and count towards your certificate.
Start the quiz →