The pythonic example I've seen not once:
{"some": "dict"}.setdefault("some", foo())
while expecting foo() to be short-circuited.
{"some": "dict"}.setdefault("some", foo())
while expecting foo() to be short-circuited.
Fun trick to learn exists. Though I would really like to know what led up to it, since it seems ... unhelpful.
Edit: Looking at other comments, I did try popping off that entry in the dict, and no, it didn't have anything in it (it doesn't make the dict anything like defaultdict).
What are people expecting with that?
conf = {}
def get_conn():
return conf.setdefault("db_conn", expensive_creation_of_db_conn())