Python Forum
mutable argument in function definition
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
mutable argument in function definition
#1
hi
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 target
after 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
Reply
#2
This is what you are doing:
default_target = []
def append_to(item, target=default_target):
    target.append(item)
    return target
When the module is first imported, a list object is created to serve as the default value for target. I explicitly do that in my example above, but functionally it is the same as what happens in your example. If you want a different list each time the function is called you need to do this:
def append_to(item, target=None):
    return [item] if target is None else target.append(item)
But you should reconsider wanting to do this at all. I'm having a hard time thinking of a use case where I want a default mutable argument that is returned as the value of the function.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  using mutable in function defintion as optional paramter akbarza 4 233 Yesterday, 12:52 PM
Last Post: deanhystad
  class definition and problem with a method HerrAyas 2 267 Apr-01-2024, 03:34 PM
Last Post: HerrAyas
  error occuring in definition a class akbarza 3 721 Nov-26-2023, 09:28 AM
Last Post: Yoriz
  determine parameter type in definition function akbarza 1 594 Aug-24-2023, 01:46 PM
Last Post: deanhystad
  [split] Explain the python code in this definition Led_Zeppelin 1 749 Jan-13-2023, 10:20 PM
Last Post: deanhystad
Information How to take url in telegram bot user input and put it as an argument in a function? askfriends 0 1,104 Dec-25-2022, 03:00 PM
Last Post: askfriends
  i want to use type= as a function/method keyword argument Skaperen 9 1,885 Nov-06-2022, 04:28 AM
Last Post: Skaperen
  Explain the python code in this definition Led_Zeppelin 1 1,106 Oct-27-2022, 04:04 AM
Last Post: deanhystad
  mutable values to string items? fozz 15 2,848 Aug-30-2022, 07:20 PM
Last Post: deanhystad
  meaning of -> syntax in function definition DrakeSoft 5 1,977 Apr-09-2022, 07:45 AM
Last Post: DrakeSoft

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020