🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← 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?

  1. A.def f(items=[]):
  2. B.def f(items=None): and create a new list inside when items is None
  3. C.def f(items=list):
  4. 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 →