473,500 Members | 1,862 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 a 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?

--
NOAGBODJI Paul Victor
Jul 15 '08 #1
2 2192
Victor Noagbodji wrote:
Why do people use if name is not None: instead of simply
writing if not name?
To differentiate from the case where name == '', or some other non-None
false value. So the question is, do you want to test for identity with
None, or for truth in general?
Jeffrey
Jul 15 '08 #2
On Jul 15, 12:44*pm, "Victor Noagbodji" <noagbodjivic...@gmail.com>
wrote:
>what's the difference between these two statement?
one checks if the given object is not None, the other checks if it's a 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?

--
NOAGBODJI Paul Victor
1. If you want to distinguish between None and something else that
evaluates to False.
2. Speed, "is not" is checking identity of the objects. That can be
(and usually is) much quicker than checking whether or not it
evaluates to True. Checking the boolean value calls the "__nonzero__"
method of an object. That method could be user defined and very
complex.

In general there are many cases where you use "None" as a placeholder
(similar to NULL in C/C++). The best practice is to always compare
identity with None.

Matt
Jul 15 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

23
2122
by: Yannick Patois | last post by:
Hi, Under some naming conditions of module files, it seems that python lost class static variables values. It seems only to append when importing a "badly" named module that itself import a...
21
1730
by: André | last post by:
Short version of what I am looking for: Given a class "public_class" which is instantiated a few times e.g. a = public_class() b = public_class() c = public_class() I would like to find...
11
21702
by: Stewart Midwinter | last post by:
I'd like to do something like the following: a = 1; b = 2; c = None mylist = for my in mylist: if my is None: print 'you have a problem with %s' % my #this line is problematic >>>...
15
2103
by: middletree | last post by:
How do I request the actual page name that I'm on? This is going to go into an include file, and depending on which page I'm on, I'd like to do different things. I'm looking for "pagename.asp"
1
5034
by: Prasad Karunakaran | last post by:
I am using the C# DirectoryEntry class to retrieve the Properties of an user object in the Active Directory. I need to get the First Name and Last Name as properties. I know it is not supported...
1
1059
by: Victor Noagbodji | last post by:
Hello, what's the difference between these two statement? And which one should one use? -- NOAGBODJI Paul Victor
0
209
by: Reedick, Andrew | last post by:
a 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...
0
163
by: Jerry Hill | last post by:
On Tue, Jul 15, 2008 at 4:13 PM, Reedick, Andrew <jr9445@att.comwrote: This is just plain untrue. If 'name is None' evaluates to true, then the variable 'name' is bound to the singleton value...
25
2744
by: Jeremy Banks | last post by:
Hi. I wondered if anyone knew the rationale behind the naming of the Popen class in the subprocess module. Popen sounds like the a suitable name for a function that created a subprocess, but the...
0
7136
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7018
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7232
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5490
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4611
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3110
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3106
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
672
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
316
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.