|
|
|
|
|
by anthony_romeo
1476 days ago
|
|
> If I'm not mistaken there's a chapter in Effective Python about this, where they recommend never using lists or dicts as Class attributes or default values for parameters and instead use factories or use None and then initialize them with default values when a new instance is created. Ya, this is because the initialized object is stored as the default parameter, and is not initialized every time the function runs. def functy(dicty={}):
print('dicty=', dicty)
return dicty
output = functy()
# dicty = {}
output.update({'key': 'value'})
functy()
# dicty = {'key': 'value'}
|
|