Dec-15-2023, 02:00 PM
(This post was last modified: Dec-15-2023, 02:00 PM by deanhystad.)
This is what you are doing:
default_target = [] def append_to(item, target=default_target): target.append(item) return targetWhen 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.