A function appends to a list passed to it. What does the caller see?
Python passes object references. Rebinding the parameter (lst = […]) would not affect the caller.
Functions · Exercise 1 of 4
Fifteen multiple-choice questions on Python functions: return and None, default and keyword arguments, *args and **kwargs, the mutable default trap, scope, global and nonlocal. Pick an option to check your answer instantly.
A function appends to a list passed to it. What does the caller see?
Python passes object references. Rebinding the parameter (lst = […]) would not affect the caller.
Why does x = 1 followed by def f(): print(x); x = 2 raise UnboundLocalError when f() is called?
Python decides at compile time which names are local. Any assignment in the body makes the name local everywhere in it.
What does f(*[1, 2], **{"c": 3}) pass to f?
* spreads a sequence as positional arguments and ** spreads a dict as keyword arguments.
Which of these is NOT possible with a Python function?
f() = … is a SyntaxError (you cannot assign to a call). Functions are first-class objects for everything else.
Why is return usually better than print inside a function?
A function that prints returns None, so x = f() * 2 fails.
Timed online tests with instant scores and explanations.