473,396 Members | 1,815 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,396 software developers and data experts.

Re: Is vs Equality Operator

Good Z wrote:
Hello,
I am having problem in using is. Here is what i am doing.
Short answer: Use == for equality. Don't use "is". Ever!
(Especially if you are a newbie.)

Longer answer: In a dozen years of programming Python, the only time I
use "is" is when testing for something like
if x is None
but, even then
if x == None
works just as well, and in any case, there is usually a better way.
(See below.)

Don't use "is" until you can explain the following
>>11 is 10+1
True
>>100001 is 100000+1
False

>
x=''
if x is None or x is '':
return 1
Both None and an empty string (and empty lists, empty dictionaries,
empty sets, and numeric values of 0 and 0.0) all evaluate a False in an
if statement, so the above if statement can be written as

if not x:
return 1

If you have to test for None, just do
if not x:

If you have to differentiate between a value of None and something that
evaluates to False (e.g., and empty list) then do
if x is None:
# handle None explicitly
elif not x:
# handle empty list
>
The above statement does not return value 1.

If i changed the above check to
if x == None or x == '':
return 1
Now it works fine.

Any idea. What is happening here. I am using python 2.4.4 on ubuntu.

Mike

------------------------------------------------------------------------
Be a better friend, newshound, and know-it-all with Yahoo! Mobile. Try
it now.
<http://us.rd.yahoo.com/evt=51733/*http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ%20>

------------------------------------------------------------------------

--
http://mail.python.org/mailman/listinfo/python-list
Jun 27 '08 #1
0 890

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

Similar topics

1
by: Doug Holland | last post by:
Currently I am responsible for writing the C# coding standards document for a client and I have been doing some investigations with ildasm to establish some additional best practices. When using...
40
by: Ike Naar | last post by:
In K&R "The C++ programming language (2nd ANSI C edition), the reference manual states (paragraphs 7.9 and 7.10) that pointer comparison is undefined for pointers that do not point to the same...
4
by: Matt Burland | last post by:
I'm a little confused about the way the default equality operator works with classes. Here's the situation, I have two comboboxes that are each filled with different object (i.e. ComboBox1 contains...
6
by: benben | last post by:
I am a C++ guy recently migrated to C#. One of the thing I don't understand is assignment (=) and equality (==) operators. If I try to do the following: C a = new C; C b = a; Then I will get...
37
by: spam.noam | last post by:
Hello, Guido has decided, in python-dev, that in Py3K the id-based order comparisons will be dropped. This means that, for example, "{} < " will raise a TypeError instead of the current...
7
by: Gary Brown | last post by:
Hi, In C#, how do you determine two objects are the "same" rather than "equal?" In C/C++ you can check the addresses and LISP provides a rich set of equality operators but C# appears ambiguous....
14
by: Robert Latest | last post by:
Hi guys, I'm sure this has been beaten to death on this newsgroup, but I can't find it in the CLC FAQ. Consider the following code: -------- double x = some_value_from_somewhere;
6
by: Edward Diener | last post by:
Now that operator overloading allows to ref classes to be compared for equality using == syntax, how does one compare the actual ref pointers ( ^ ) for equality instead ? As an example: ...
3
by: toton | last post by:
Hi, I have a struct Point { int x, int y; } The points are stored in a std::vector<Pointpoints; (global vector) I want to add equality (operator == ) for the point, which will check equality...
16
by: DamienS | last post by:
In the interests of me saving hair, can someone please explain to me what's going on below? Why doesn't == work in comparing two int's when cast as objects? They're the same type. Note that it...
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: 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: 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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
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,...

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.