3 ms·
There are a few use cases for default variables on effbot's site: http://effbot.org/zone/default-values.htm http://effbot.org/zone/default-values.htm Basically
by anthonyb 14y ago
There are a few use cases for default variables on effbot's site: http://effbot.org/zone/default-values.htm http://effbot.org/zone/default-values.htm
Basically, sometimes you do want to reuse the mutable between function calls, and in those cases it can save a fair bit of code passing it in repeatedly.
- iki23 14y agoGood coverage. I use it quite often for cache dictionary, it's much simpler API and overall code, than creatng a new class for it. Demo snippet from effbot's site: def calculate(a, b, c, memo={}): try: value = memo[a, b, c] # return already calculated value except KeyError: value = heavy_calculation(a, b, c) memo[a, b, c] = value # update the memo dictionary return value
- ollysb 14y agoThis seems a bit leaky. You're exposing the caching mechanism in the method signature(yeah, ok, in practice it's unlikely to be a problem).
- anthonyb 14y agoThe other option is to create your own cache object and pass that in (and around, if it's a recursive function). Of course, in Python pretty much every cache object follows the dictionary interface anyway, so it doesn't really matter. One of the benefits of duck typing :)