PY
py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#The hidden trap in python function
#What will be the output of the following Python code?
def install(package):
__import__('os').system("pip install -qq --target='/usercode' " + package)
install("icecream")
from icecream import ic
def func(a, b=[]):
ic(a)
b.append(a)
# a keeps being appended to b list - here is the explanation using ic as to what is happenning
return ic(b)
print(func(1))
print(func(2))
print(func(3, []))
# Here, we're passing a new, empty list [] as the argument for b. This overrides the default argument. b becomes [3].
print(func(4))
Enter to Rename, Shift+Enter to Preview
OUTPUT
Run