Dec-15-2023, 10:54 AM
hi
the below code is in :
https://realpython.com/python-mutable-vs...-a-summary
in the below code:
On the site is written that:
thanks
the below code is in :
https://realpython.com/python-mutable-vs...-a-summary
in the below code:
def append_to(item, target=[]): target.append(item) return targetafter running, What you might expect to happen:
append_to(1)# expected output :[1]
append_to(2)# expected output :[2]
append_to(3)# expected output:[3]
On the site is written that:
Quote:Because Python defines the default argument value when it first parses the function and doesn’t overwrite it in every call, you’ll be working with the same instance every time. Then, you don’t get the above output. Instead, you get the following output:# What actually happens:
append_to(1)# output is
Output:[1]
append_to(2)# output is
Output:[1,2]
append_to(3)# output is Output:[1,2,3]
I did not understand what was written there. can explain it to me?thanks