231

I put a dict as the default value for an optional argument to a Python function, and pylint (using Sublime package) told me it was dangerous. Can someone explain why this is the case? And is a better alternative to use None instead?

wjandrea
  • 28,235
  • 9
  • 60
  • 81
tscizzle
  • 11,191
  • 15
  • 54
  • 88
  • 4
    The problem with pasing empty list as a default argument is that it will be shared between all invocations of the function -- see the "important warning" in https://docs.python.org/3/tutorial/controlflow.html#default-argument-values – hamed Jan 18 '17 at 12:13

2 Answers2

293

Let's look at an example:

def f(value, key, hash={}):
    hash[value] = key
    return hash

print(f('a', 1))
print(f('b', 2))

Which you probably expect to output:

{'a': 1}
{'b': 2}

But actually outputs:

{'a': 1}
{'a': 1, 'b': 2}
Francisco
  • 10,918
  • 6
  • 34
  • 45
Bill Lynch
  • 80,138
  • 16
  • 128
  • 173
  • 27
    so how we can still achieve a default value that matches afterwards code, vs saying "if h is None: h = {}" and then continue the code ? – Ricky Levi Jun 12 '19 at 09:49
  • 14
    I am kinda late, but I wanted to say thanks for this example. This was not something I was aware of with Python, so I was wondering why having empty list/dict was considered "dangerous". This helped a lot. – Mandemon Oct 20 '20 at 09:12
  • so `hash` becomes a global variable? – alper Jan 26 '21 at 22:23
  • 4
    @alper it's not global variable, you can access it only in the scope of the function, but the default value, if not supplied is referenced to the dict you defined as literal. one way to avoid it is to def func(hash=None): if hash == None: hash = {} ... – marxus Jan 31 '21 at 17:51
  • 23
    I just want to say this is a real Python WTF. Who thought this could possibly be sane behaviour? – Timmmm Aug 12 '21 at 11:57
  • Just bitten me as well. Tests run individually work but as a collection prior calls contaminate later ones. – hum3 Sep 23 '21 at 15:36
  • This is a nice example, but AFAICT `hash` shouldn't be in the list of parameters when you only want the "expected output", above. – Mike Pennington Jun 10 '22 at 14:32
  • 1
    @RickyLevi you can achieve such possiblity by replacing the arg using list or dict `.copy()` or `copy.deepcopy(obj)` before using the arg in the the function. So original list or dict stay safe since you're messing with a copy of it, not the original one. – Ratul Hasan Jun 12 '22 at 22:23
  • 10
    Note that `hash=dict()` has the same behaviour as `hash={}`. – aydow Oct 13 '22 at 12:00
  • 1
    @RickyLevi I'm not sure what you mean by "afterwards code". But, I don't think you can safely achieve what you want. To be safe and write robust code, you do exactly what you suggest (as per `marxus's` comment): set the default to `None` and then, in the body of the function, if the arg is `None`, assign an empty `dict`. That's it. Otherwise, you may have the problem that this answer describes. – Hawkeye Parker Mar 28 '23 at 20:50
  • 1
    @HawkeyeParker sorry , I meant the code that comes inside the function - after the function header where i define arguments ... – Ricky Levi Mar 29 '23 at 22:02
270

It's dangerous only if your function will modify the argument. If you modify a default argument, it will persist until the next call, so your "empty" dict will start to contain values on calls other than the first one.

Yes, using None is both safe and conventional in such cases.

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
  • 2
    if function is not modifying the argument, should we still use None as default in the name of best practice? – NightFurry Sep 29 '17 at 06:43
  • 2
    @NightFurry: I hesitate to prescribe anything here--`None` is often the best choice, but perhaps not 100% of the time. Use your judgement, but if you can't decide, `None` is a safe bet. – John Zwinck Sep 30 '17 at 01:11
  • 8
    In case anyone else stumbles across this question, please look at [this](https://stackoverflow.com/a/51710151/11106639) answer on optional dictionaries in function arguments. I feel it's a much better discussion on how this issue should be addressed. Basically, today one should type-hint an optional mapping of {key: value}. ie `arg: Optional[Mapping[str, str]] = None` – Inarus Lynx Jan 19 '21 at 15:08
  • If the function does not modify the argument but leaks a reference to it (by returning it, yielding it, calling some other function with it as an argument, or so on), then the same problem can occur. I would recommend *always* using `None`, even if the function is written carefully to avoid such bugs, purely so that seemingly-innocuous changes to the function cannot break it. – kaya3 Mar 28 '21 at 03:41
  • 1
    That sounds like a bug in python, as far I know other dynamic programming languages will not behave that way – 27px May 18 '22 at 11:09