python - Pythonic way of setting a default -
is there simple way of of setting default in python - setting default in dict?
for instance, let's have dict called foo, may or may not have assigned on key bar. verbose way of doing is:
if not foo.has_key('bar'): foo['bar'] = 123 one alternative be:
foo['bar'] = foo.get('bar',123) is there standard python way of doing - following, works?
foo['bar'] ||= 123
doesn't read the documentation?
foo.setdefault('bar', 123)
Comments
Post a Comment