r/Python • • Jul 22 '21

Resource Do not use mutable objects as default arguments in Python

https://medium.com/geekculture/do-not-use-objects-as-default-arguments-in-python-1c940212db2e
1.3k Upvotes

218 comments sorted by

View all comments

Show parent comments

0

u/[deleted] Jul 23 '21

[deleted]

1

u/[deleted] Jul 23 '21

allows the input arg to be optional which doesn't make sense. An append function by definition must mutate the input. Therefore has the responsibility of returning the same object as the one input (exceptions applies like Pandas dataframe who always return a new obj). I hope you agree with me that functions that mutate the input should not be an optional arg.

I absolutely agree with you. I was merely using those functions to demonstrate behavior as they are in line with what the thread was talking about.

(exceptions applies like Pandas dataframe who always return a new obj)

I'm being pedantic, but that's the default but it can mutate the existing dataframe by passing inplace=True to most methods.

I'm not trying to say you are wrong or anything, and it sounds like you have a good enough understanding that you probably know when checking for truthiness makes sense vs checking identity.

My point is for those who may not be aware of the difference so that they can be sure to use them in the correct circumstances. I only know this matters myself from experience. It's not just about being idiot-proof. Although I'm probably not explaining it very well.

My job revolves around automating network devices. In these situations, you cannot guarantee much about the data coming into the application. I can give you a specific example of when this could actually cause problems in code.

Lets say I need to do some processing on a bunch of network device interfaces. Due to the nature of this process, it's virtually impossible to program for every possible way these interfaces will look, but we do our best to extract and interface name and an interface description. After collecting this data, we have something that looks like this:

interfaces = [
    ("Eth1/1", "Router1"),
    ("Eth1/2", "Router2"),
    ("Eth1/3", ""),
    ("Eth1/4", None)
    ]

Now, I need to process this data and convert the tuples into a list of dictionaries. In the event we failed to parse the interface description, we should set the description to "N/A". However, an empty string is a valid value, that just means there is no description on that interface. So, using the method your describing, here is my little function that handles the data:

def process_data(interface, description=None):
    description = description or "N/A"
    data = {
        "interface": interface,
        "description": description
        }
    return data

parsed_interfaces = [process_data(i, description=d) for i, d in interfaces]
pprint(parsed_interfaces)

So now the result of the function will look like this:

[{'description': 'Router1', 'interface': 'Eth1/1'},
 {'description': 'Router2', 'interface': 'Eth1/2'},
 {'description': 'N/A', 'interface': 'Eth1/3'},
 {'description': 'N/A', 'interface': 'Eth1/4'}]

Now Eth1/3 shows "N/A" but that's not what we want, only Eth1/4 should show N/A.

So, updating the process_data function to check identity instead of truth value and running again:

def process_data(interface, description=None):
    if description is None:
        description = "N/A"
    data = {
        "interface": interface,
        "description": description
        }
    return data

parsed_interfaces = [process_data(i, description=d) for i, d in interfaces]
pprint(parsed_interfaces)

[{'description': 'Router1', 'interface': 'Eth1/1'},
 {'description': 'Router2', 'interface': 'Eth1/2'},
 {'description': '', 'interface': 'Eth1/3'},
 {'description': 'N/A', 'interface': 'Eth1/4'}]

Now we have the behavior we are looking for. It has nothing to do with being idiot-proof nor does it require type checking the input.

Hopefully that better illustrates the point I'm making and why it matters. Oh, and if you really want it to be a one-liner, that's still easy to do with description = "N/A" if description is None else description but I would personally shy away from that as it isn't as readable IMO.