-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquestion_4_answer.txt
15 lines (11 loc) · 1011 Bytes
/
question_4_answer.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
4. Is there any issue with the following code that may alterthe expected behaviour?
It really depends on what do you mean by expected behaviour. Maybe you want to cache calls of this functions when no list is passed
as an argument?
I reckon this is about functions in Python being objects that are evaluated on their definition and default arguments being persisted
when they're mutable. Default parameters are some sort of 'object attributes' and, because of this, their state may be updated
in each call - as any other object.
In other words, I believe that the 'unexpected behaviour' you are expecting me to point out has to be the fact that list_ is
a mutable object defined as a default argument and, therefore, this list will not be empty the next time the function is called -
it'll have the previous entries appended on the previous calls.
Of course, this will not happen if the default argument is overwritten. Then, the list will be initialized as the one passed as
an argument of this function.