Pourquoi les éléments suivants:Initialisation du args mot-clé en Python
class A(object):
def __init__(self, var=[]):
self._var = var
print 'var = %s %s' % (var, id(var))
a1 = A()
a1._var.append('one')
a2 = A()
résultat dans:
var = [] 182897439952
var = ['one'] 182897439952
Je ne comprends pas pourquoi il n'utilise une nouvelle instance d'une liste lors de l'utilisation en option arguments de mot-clé, quelqu'un peut-il expliquer cela?
Il a été expliqué encore et encore. – SilentGhost
Exemple d'explication: http://stackoverflow.com/questions/530530/python-2-x-gotchas-and-landmines –
ou ici: http://stackoverflow.com/questions/366422/what-is-the- pythonic-way-to-avoid-default-parameters-that-are-empty-lists – SilentGhost