5 ms·
> I'm not familiar with python - does it have some magic global sentinel value to indicate "not found"? No, it has a different method for failable search. The
by masklinn 16y ago
> I'm not familiar with python - does it have some magic global sentinel value to indicate "not found"?
No, it has a different method for failable search. The purpose of `get` is specifically to handle "provide a default value in case the key is not found".
> Or how do you handle that case with that 1 line of code?
dict[key]
- xiongchiamiov 16y agoYou can also use defaultdict[0], or define the __missing__() method[1] yourself on a dictionary you receive from someone else's code. In either of these cases the "not found" value will be whatever you decide it should be. [0]: http://docs.python.org/library/collections.html#collections.defaultdict http://docs.python.org/library/collections.html#collections.... [1]: http://docs.python.org/library/stdtypes.html#dict http://docs.python.org/library/stdtypes.html#dict