我使用a dict作为短期缓存.我想从字典中获取一个值,如果字典中没有该键,则设置它,例如:
dict
val = cache.get('the-key', calculate_value('the-key')) cache['the-key'] = val
在'the-key'已经存在的情况下cache,第二行不是必需的.对此有更好,更短,更具表现力的习语吗?
'the-key'
cache
python
python ×1