Python

Functions: Parameters & Scope

28 flashcards · answers and spaced-repetition review in the KnowCard app

What does this print? ``` def f(x, acc=[]): acc.append(x) return acc print(f(1)) print(f(2)) ```
When is a parameter's default value expression actually evaluated?
What does this print? ``` def f(a=[1, 2, 3]): a += [4, 5] print(a) f() f() ```
In a function definition, where must parameters with default values be placed, and why?
A function ends without ever executing a return statement. What does calling it evaluate to?
What does this print, and why? ``` def my_sum(a, b, c=0, d=0): return a + b + c + d print(my_sum(d=1, b=3, c=2, a=1)) ```
Is this call legal? Why or why not? ``` def my_sum(a, b, c=0, d=0): return a + b + c + d my_sum(1, c=10, 2) ```
What does this print? ``` def f(a, b, *other): print(a, b, other) f(1, 2, 3, 4) f(1, 2) ```
In def f(a, **kwargs):, what type is kwargs and what does it hold?
What is the correct parameter order when a function combines fixed params, *args, keyword-only params, and **kwargs?
What happens here, and how do you call f successfully? ``` def f(a, b, *, c, d): print(a, b, c, d) f(1, 2, 3, 4) ```
What does the `/` do here, and which call is illegal? ``` def f(a, b, /, c, d): print(a, b, c, d) f(1, b=2, c=3, d=4) f(1, 2, c=3, d=4) ```
What does this print? ``` def my_sum(a, b, c=0, d=0): return a + b + c + d t = (6, 3, 9, 12) print(my_sum(*t)) ```
What does this print? ``` def my_sum(a, b, c=0, d=0): return a + b + c + d d = {"a": 7, "b": 3, "c": 4} print(my_sum(**d)) ```
Mutating an argument inside a function sometimes changes the caller's object and sometimes doesn't. What determines which?
What does this print? ``` def f(lst): lst += [5, 6] nums = [1, 2] f(nums) print(nums) ```
A name is read in a function and Python finds the value. In what order does it search scopes?
What does this print? ``` name = "Peter" def hello(): print(name) name = "Jo" hello() ```
Inside a function you want to reassign a module-level variable. What happens without, and with, global?
What does this print, and what keyword makes it work? ``` def outer(): res = 1 def inner(): res += 1 inner() print(res) outer() ```
Are Python functions first-class objects? What does that let you do?
A nested function has its own namespace and can't see the enclosing function's locals. How can you still hand it an enclosing value at definition time (without nonlocal)?
You must pass a list to a function that mutates its argument, but you don't want the caller's list changed. What's the fix?
What do the built-in globals() and locals() functions return, and how do they relate in the main program?
A = [1, 2, 3] print([1, *A, *A]) print({1, *A, *A}) print({"a": 10, **{"a": 11}, "a": 13}) What does each line produce?
``` def fac(n): if n < 0: return None result = 1 for i in range(2, n + 1): result *= i return result ``` Why does a negative n never reach the loop?
``` def my_sum(a, b, c=0, d=0): return a + b + c + d my_sum(*(1, 2), *(3, 4)) my_sum(1, *(2, 3), **{"d": 4}) ``` Are both calls legal, and what do they return?
Your code uses def f(a, b, /, c, d): and crashes with a SyntaxError on a colleague's older machine, but runs fine on yours. Likely cause?

Start learning today

Free to start — download the app or use it in your browser.

Get it on App StoreGet it on Google Play