Python Forum
using mutable in function defintion as optional paramter
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
using mutable in function defintion as optional paramter
#5
Maybe this makes it more clear.
def incorrect(item, quantity, shopping_list={}):
    shopping_list[item] = shopping_list.get(item, 0) + quantity
    return shopping_list  # Is needed else there is no way to get the "default" shopping list


default_dictionary = {}
def whats_happening(item, quantity, shopping_list=default_dictionary):
    shopping_list[item] = shopping_list.get(item, 0) + quantity
    return shopping_list  # Is needed else there is no way to get the "default" shopping list


def correct(item, quantity, shopping_list=None):
    if shopping_list is None:
        shopping_list = {}
    shopping_list[item] = shopping_list.get(item, 0) + quantity
    return shopping_list  # Is needed else there is no way to get the "default" shopping list


def test(func):
    a = func("Shirt", 3)
    b = func("USB cable", 1)
    print(func.__name__, a, b, "a is b" if a is b else "a is not b")


test(incorrect)
test(whats_happening)
test(correct)
Output:
incorrect {'Shirt': 3, 'USB cable': 1} {'Shirt': 3, 'USB cable': 1} a is b whats_happening {'Shirt': 3, 'USB cable': 1} {'Shirt': 3, 'USB cable': 1} a is b correct {'Shirt': 3} {'USB cable': 1} a is not b
Notice that incorrect() and whats_happening() have the same issue. The default dictionary gets reused over and over instead of creating a new dictionary each time. Dictionary a and dictionary b are the same object. Why this happens is easier to see when the dictionary is created explicitly, but explicit or implicit the result is the same. One dictionary is created as the default, and that same dictionary is used every time it is needed. In your example, every dictionary created by calling add_item() without a shopping list returns the same dictionary object.
akbarza likes this post
Reply


Messages In This Thread
RE: using mutable in function defintion as optional paramter - by deanhystad - Apr-26-2024, 12:52 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  mutable argument in function definition akbarza 1 597 Dec-15-2023, 02:00 PM
Last Post: deanhystad
  mutable values to string items? fozz 15 3,201 Aug-30-2022, 07:20 PM
Last Post: deanhystad
  "'DataFrame' objects are mutable, thus they cannot be hashed" Mark17 1 7,035 Dec-25-2020, 02:31 AM
Last Post: tinh
  Mutable Strings millpond 3 2,727 Aug-24-2020, 08:42 AM
Last Post: millpond
  What is the meaning of mutable data type? qliu 3 3,104 Apr-17-2020, 07:20 PM
Last Post: deanhystad
  copying parts of mutable sequences Skaperen 1 2,318 Dec-02-2019, 10:34 AM
Last Post: Gribouillis
  A mutable string?? silentknight85 5 4,843 May-31-2019, 10:11 AM
Last Post: silentknight85
  Trouble making an argument optional linuxnoob 2 3,015 Aug-31-2018, 01:52 AM
Last Post: linuxnoob
  compacting a mutable sequence Skaperen 6 4,606 Jan-23-2018, 03:54 AM
Last Post: Skaperen
  Paramter lists when shift+tab doesn't work sobrio1 0 3,237 Oct-15-2017, 03:41 PM
Last Post: sobrio1

Forum Jump:

User Panel Messages

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