'if name is not None:' v. 'if name:'
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:
> >>And which one should one use?
> >depends on what you want to test for, of course.
> 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
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.
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.
d = dict()
if not d:
d['a'] = 1
d = None
if not d:
d['c'] = 3
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