On May 25, 2013, at 12:23 PM, Michael Ströder <michael@stroeder.com> wrote:
Thomas Lehmann wrote:
def foo(var = {}):
Ohne jetzt genau auf die Frage einzugehen, was andere ja schon gemacht haben:
Ist ein mutable Default-Wert für ein key-word argument nicht ein absolutes no-go? Zumindestens musste ich das selbst mal auf die harte Tour lernen und vor allem kann es je nachdem sicherheitsrelevant werden.
Ja, das ist gefährlich.
Besser wäre
def foo(var = None): var = var or {} [..]
Dann wird nämlich jedes Mal eine neue Dictionary-Instanz erzeugt, falls nur foo() aufgerufen wird.
Und noch besser wäre var = {} if var is None else var seit Python 2.5. Denn dieses Gefummel mit or und and statt dem ternaeren if führt ebenfalls zu seiner ganz eigenen Klasse von Fehlern. Diez