No, I believe that the form you have presented is minimal. I cannot talk about Python 3, because I have no experience.
(Negative responses are complicated, but I have no supporting evidence, except for the absence of evidence to the contrary...)
Updating: as 3 people have already posted the same incorrect answer, I will point it out here to avoid future identical answers.
>>> def uma_funcao(um_dict={}):
... um_dict['uma_chave'] = True
... return um_dict
...
>>> x = uma_funcao()
>>> x['teste'] = True
>>> y = uma_funcao()
>>> y
{'uma_chave': True, 'teste': True}
As you can see, you shouldn’t use a changeable value as standard parameter of a function. The expression {}
creates an object before to be used as a parameter, so that every call to uma_funcao
without passing parameters will use the same dict
. As rarely this is the desired behavior, there is no option but the use of None
followed by a test, as in the original question code.
Another way that occurred to me, very common in languages like C, was to combine attribution with use in a single expression:
(um_dict = um_dict or {})['uma_chave'] = True
However this is not allowed by the Python syntax (2 at least; if something like this has been added in future versions, it is not to my knowledge).
The reason is that the same
dict
will be used in every invocation ofuma_funcao
[no arguments], but the expected behavior (intuitive until) would be that a new Dict be created at each invocation.– mgibsonbr