[Python-ideas] Maybe/Option builtin

Steven D'Aprano steve at pearwood.info
Thu May 22 02:24:34 CEST 2014


On Wed, May 21, 2014 at 05:11:19PM -0700, Devin Jeanpierre wrote:

> Python has no way to represent "I have a value, and that value is None".

Sure it has.  'a' in x and x['a'] is None


> e.g. what is the difference between x.get('a') and x.get('b') for x ==
> {'a': None} ?

dict.get is explicitly designed to blur the distinction between "I have 
a key and here is its value" and "I may or may not have a key, it 
doesn't matter which, return its value or this default regardless". (The 
default value defaults to None.) If you care about the difference, as 
most people do most of the time, you should avoid the get method and 
call x['a'] directly.



-- 
Steven


More information about the Python-ideas mailing list