Dec-15-2023, 10:54 AM
hi
the below code is in :
https://realpython.com/python-mutable-vs...-a-summary
in the below code:
after running, What you might expect to happen:
# expected output :[1]
# expected output :[2]
# expected output:[3]
On the site is written that:
# output is
)# output is
# output is
thanks
the below code is in :
https://realpython.com/python-mutable-vs...-a-summary
in the below code:
1 2 3 |
def append_to(item, target = []): target.append(item) return target |
1 |
append_to( 1 ) |
1 |
append_to( 2 ) |
1 |
append_to( 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:
1 |
append_to( 1 ) |
Output:[1]
1 |
append_to( 2 |
Output:[1,2]
append_to(3)1 |
|
Output:[1,2,3]
I did not understand what was written there. can explain it to me?thanks