473,385 Members | 2,003 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 2176
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
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
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
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
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
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
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
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
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
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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
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
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...

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.