No, I believe that this form you presented is already minimal. I can not talk about Python 3, as I have no experience.
(Answers in the negative are complicated, but I have no evidence to support it except the absence of evidence to the contrary ...)
Update: As 3 people have already posted the same incorrect answer, I'll point it out here to avoid future identical responses.
>>> 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 should not use a changeable value as a default parameter for a function. The expression {}
creates a before object to use as a parameter, so any call of uma_funcao
without passing parameters will use the same < dict
. Since this is seldom the desired behavior, there is no option but to use None
followed by a test, just like the original question code.
Another way I thought of it, very common in languages like C, was to merge the assignment with use into a single expression:
(um_dict = um_dict or {})['uma_chave'] = True
However this is not allowed by the Python syntax (at least 2, if something like this has been added in future versions, it is not known to me).