[issue38998] dict.setdefault (setdefault of dictionary)

2019-12-08 Thread da-dada
da-dada added the comment: my use case is different (I do a loop), but what I expected from the docs (just for fun!) class Ddefault: def __init__(self): vars(self).setdefault('default', self.set_default() if not 'default' in vars(self) else self.default) vars(self).setde

[issue38998] dict.setdefault (setdefault of dictionary)

2019-12-08 Thread Rémi Lapeyre
Rémi Lapeyre added the comment: The documentation is correct, in Python argument are computed before the call to a function, not when they are used. You can try other functions than dict.setdefault() and see that the behaviour is always the same. Le dim. 8 déc. 2019 à 22:47, da-dada a écrit :

[issue38998] dict.setdefault (setdefault of dictionary)

2019-12-08 Thread da-dada
da-dada added the comment: I have no problem of making my programme run properly (asking first if in dict etc), but I just read the docu of dict.setdefault setdefault(key[, default]) If key is in the dictionary, return its value. If not, insert key with a value of default and return default

[issue38998] dict.setdefault (setdefault of dictionary)

2019-12-08 Thread Eric V. Smith
Eric V. Smith added the comment: Right. If you want the value only calculated once, then just call it once. You might be interested in collections.defaultdict, which takes a factory function, and only calls it as needed. -- nosy: +eric.smith resolution: -> not a bug stage: -> resol

[issue38998] dict.setdefault (setdefault of dictionary)

2019-12-08 Thread Rémi Lapeyre
Rémi Lapeyre added the comment: > > > def __init__(self): > vars(self).setdefault('default', self.set_default()) > vars(self).setdefault('default', self.set_default()) > This code is equivalent to def __init__(self): x = self.set_default() vars(self).set

[issue38998] dict.setdefault (setdefault of dictionary)

2019-12-08 Thread da-dada
New submission from da-dada : from the docu I expected at the second call just a return of value and not a second calculation: there is room for improvement, as Elon Musk would say.. class Ddefault: def __init__(self): vars(self).setdefault('default', self.set_default()) v