Re Being Pythonic

Stephen J. Turnbull stephen at xemacs.org
Sat Apr 18 14:03:50 BST 2009


Russel Winder writes:

 > OK I am intrigued why is:
 > 
 > 	if filter is None : . . . 
 > 
 > more Pythonic than:
 > 
 > 	if filter == None : . . . 

Categorical thinking.  "None" isn't really a value, not even a empty
value.  It's the absence of value, and as such is conceptually unique,
and in Python it is implemented as the unique object of the None type.
It's not the same as an empty list or 0 or other false object,
although it is false in a Boolean context.  You don't produce None by
a cancellation operation; you produce None by failing to operate.  So
it's different from Lisp's nil.

Note that many Lisps have a `voidp' predicate to express what is
Pythonically expressed by "is None".  The variable doesn't have an
empty value, it was never assigned and so has *no* value.  Python has
many empty values: (), [], {}, which have different types.  In fact
the expression "[] is []" evaluates to "False" because there are as
many empty lists as you care to create, although they are all equal:
"[] == []" evaluates to True.  And that needs to be the case, since if
all empty lists were the same object, then

    a = []
    b = []                # the same unique empty list?!
    a.insert('a')
    b.insert('b')         # updates the same (no longer empty) empty list!!
    print(b)

would print "['b', 'a']", a somewhat unintuitive result, and one that
would make life difficult for any application that wanted to
accumulate lists of several different kinds of objects.

So None is very special.  It is generally used as a sentinel that says
"unask the question, because it was the wrong one; there is no answer
to that question."  The value *is* missing; it's weird to say the
value "equals" missing,

And that is the case here.  If filter is None, that means special or
default handling is needed.  It is a sentinel for that purpose.




More information about the bazaar mailing list