Actually, that's close to what I'd probably write (though not always; context is everything, and I'd do "except KeyError" not bare except, of course). Though the reason for that is that I wouldn't call the variable tmpvar; I'd give it a meaningful name, and think of it as an opportunity to write more self-documenting code.
I don't think I've ever come across this in practice, though; normally, I just work with dicts which either don't contain None, or where None would be expected to be treated the same as the entry being missing.
If I was writing some complicated boolean expression along the lines ofand realised that the dictionary might not contain the key at all, then I'd much rather correct the code tothan have to turn it into something like this:
try: tmpvar = dict[key]==value except: tmpvar = False if a<b and c==d and (tmpvar or other_equally_good_case):There just doesn't seem to me to be any contest there.I don't think I've ever come across this in practice, though; normally, I just work with dicts which either don't contain None, or where None would be expected to be treated the same as the entry being missing.