what exactly is "None" ?

Erik Max Francis max at alcyone.com
Tue Mar 4 17:48:51 EST 2003


Andrei Doicin wrote:

> I know that "None" is the Boolean false value that
> gets thrown out when there's nothing to return or a
> function doesn't manage to reach the end of itself and thus "return
> whatever", but *what* (in Python terms) exactly is equal to "None" ???
> 
> I ask this as I'm trying to set up some conditional statements that do
> something if "whatever" is equal to "None".

None is often used as the equivalent of NULL or null in other languages;
it's a unique value (and a singleton, so None is always the same object
unless someone's playing tricks), and it's usually used to indicate the
willful and intentional lack of some other object.  To test for
None-ness, use the is operator.  The re.search and re.match functions
(and methods) return a match object, or None.

None is also frequently found in default arguments where the default
argument would be mutable (and thus might lead to unexpected behavior,
since default arguments are only evaluated and stowed once).  The best
way to test for Noneness is with the `is' operator:

	def app(x, l=None):
	    if l is None:
	        l = [] # this way a unique empty list gets returned
	    l.append(x)
	    return l

-- 
 Erik Max Francis / max at alcyone.com / http://www.alcyone.com/max/
 __ San Jose, CA, USA / 37 20 N 121 53 W / &tSftDotIotE
/  \ The people are to be taken in very small doses.
\__/ Ralph Waldo Emerson
    Alcyone Systems / http://www.alcyone.com/
 Alcyone Systems, San Jose, California.




More information about the Python-list mailing list