'if name is not None:' v. 'if name:'

Reedick, Andrew jr9445 at ATT.COM
Tue Jul 15 22:13:18 CEST 2008

> -----Original Message-----
> From: python-list-bounces+jr9445=att.com at python.org [mailto:python-
> list-bounces+jr9445=att.com at python.org] On Behalf Of Victor Noagbodji
> Sent: Tuesday, July 15, 2008 3:44 PM
> To: python-list at python.org
> Subject: Re: 'if name is not None:' v. 'if name:'
> >>what's the difference between these two statement?
> >one checks if the given object is not None, the other checks if it's
> true value:
> >http://docs.python.org/ref/Booleans.html#Booleans
> >>And which one should one use?
> >depends on what you want to test for, of course.
> >
> ></F>
> Well that's exactly why I'm asking. Since None returns False in if
> statements. Why do people use if name is not None: instead of simply
> writing if not name?

If name is None:
    Then name is NULL, nothing, nada, no object, no memory allocated, a
NULL pointer

If name is not None:
    Then name is an object.  It's a pointer to some kind of allocated
structure in memory.  No idea if it contains a false or true value.

If name:
    Then either
        a) name is an object, and that object does not have a 'false'
value, such as False, zero, or empty.
        b) name is NULL/None.  No object.

Try this:

d = dict()
if not d:
	d['a'] = 1
print d

d = None
if not d:
	d['c'] = 3
print d


The information transmitted is intended only for the person or entity to which it is addressed and may contain confidential, proprietary, and/or privileged material. Any review, retransmission, dissemination or other use of, or taking of any action in reliance upon this information by persons or entities other than the intended recipient is prohibited. If you received this in error, please contact the sender and delete the material from all computers. GA622

More information about the Python-list mailing list