473,385 Members | 1,919 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.

comparison with None

>>None >= 0
False
>>None <= 0
True

Explanation appreciated.

Thanks,
Alan Isaac
Apr 18 '07 #1
18 4964

"Alan G Isaac" <ai****@american.eduwrote in message
news:3I******************************@rcn.net...
| >>None >= 0
| False
| >>None <= 0
| True
|
| Explanation appreciated.

Should be in the reference manual section on comparisons.

Apr 18 '07 #2
Alan G Isaac wrote:
>>None >= 0
False
>>None <= 0
True

Explanation appreciated.

Thanks,
Alan Isaac
I've read and found that 'None' comparisons is not always a good idea.
Better to:
from types import NoneType

x = None
if type( x ) == NoneType:
# true
< code >
else:
# false; do something else.
< more code >

Steven Howe

Apr 18 '07 #3
On Apr 18, 5:19 pm, Steven Howe <howe.ste...@gmail.comwrote:
Alan G Isaac wrote:
>>None >= 0
False
>>None <= 0
True
Explanation appreciated.
Thanks,
Alan Isaac

I've read and found that 'None' comparisons is not always a good idea.
Better to:
from types import NoneType

x = None
if type( x ) == NoneType:
# true
< code >
else:
# false; do something else.
< more code >

Steven Howe
None is a singleton - there is but one None and no other. The only
comparisons that make sense with None are "is" or "is not". type(x)
== NoneType is unnecessary, x is None is sufficient.
>>x = None
x is None
True
>>y = None
x is y
True
>>z = object()
z is None
False
>>z is not None
True
>>x is not None
False
>>y is not None
False

-- Paul
Apr 18 '07 #4
On Apr 18, 3:19 pm, Steven Howe <howe.ste...@gmail.comwrote:
I've read and found that 'None' comparisons is not always a good idea.
Better to:
from types import NoneType

x = None
if type( x ) == NoneType:
# true
< code >
else:
# false; do something else.
< more code >

Steven Howe
Is that any better than this?

if x is None:
# do something
else:
# do something else

Apr 18 '07 #5
br*****@gmail.com wrote:
On Apr 18, 3:19 pm, Steven Howe <howe.ste...@gmail.comwrote:
>I've read and found that 'None' comparisons is not always a good idea.
Better to:
from types import NoneType

x = None
if type( x ) == NoneType:
# true
< code >
else:
# false; do something else.
< more code >

Steven Howe

Is that any better than this?

if x is None:
# do something
else:
# do something else
To the contrary, it's not as good. "if x is None" is much clearer, and
probably faster.
--
Michael Hoffman
Apr 18 '07 #6
Alan G Isaac wrote:
>>None >= 0
False
>>None <= 0
True

Explanation appreciated.

Thanks,
Alan Isaac
So that we can sort lists of objects, even when the objects of are
different types, Python guarantees to supply a unique and consistent
ordering of any two objects. The definition of Python does not specify
what that ordering is -- that's implementation dependent -- but any two
objects of any two types *do* have an ordering and that ordering will
always be the same.

So in your implementation None is less than 0 (and probably less than
any integer). Given that, your two observations above are consistent.

Gary Herron

Apr 18 '07 #7
br*****@gmail.com wrote:
On Apr 18, 3:19 pm, Steven Howe <howe.ste...@gmail.comwrote:
>I've read and found that 'None' comparisons is not always a good idea.
Better to:
from types import NoneType

x = None
if type( x ) == NoneType:
# true
< code >
else:
# false; do something else.
< more code >

Steven Howe

Is that any better than this?

if x is None:
# do something
else:
# do something else
No. using
if x is None:
is the recommended way.

Gary Herron

Apr 18 '07 #8
Steven Howe wrote:
Alan G Isaac wrote:
> >>None >= 0
False
> >>None <= 0
True

Explanation appreciated.

Thanks,
Alan Isaac
I've read and found that 'None' comparisons is not always a good idea.
Better to:
from types import NoneType

x = None
if type( x ) == NoneType:
# true
< code >
else:
# false; do something else.
< more code >
The recommended idiom is to test for "x is None".

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

Apr 18 '07 #9
"Terry Reedy" <tj*****@udel.eduwrote in message
news:ma***************************************@pyt hon.org...
Should be in the reference manual section on comparisons.
Only to this extent:
http://www.python.org/doc/2.4/ref/comparisons.html

objects of different types always compare unequal, and are ordered
consistently but arbitrarily.

(This unusual definition of comparison was used to simplify the
definition of operations like sorting and the in and not in
operators.
In the future, the comparison rules for objects of different types
are
likely to change.)

... Most other types compare unequal unless they are the same
object;
the choice whether one object is considered smaller or larger than
another one is made arbitrarily but consistently within one
execution
of a program.

This does not provide a direct answer to "why" None comparisons.
(As far as I can tell, None is less than any object.)

However, Gary Herron's explanation makes sense: this provides a stable
sort when None is involved, and meets the criterion that objects of
different types must always compare unequal. However this would also
be true if None always compared greater than any object, and the current
behavior does not seem to be guaranteed.

Is that about right?

Cheers,
Alan Isaac
Apr 19 '07 #10
On Wed, 18 Apr 2007 15:19:26 -0700, Steven Howe wrote:
I've read and found that 'None' comparisons is not always a good idea.
You're probably thinking of testing against None with equality:

if x == None: do_something()

That can go wrong if x is a class that has an overly-broad concept of
equality, e.g.:

class FalseKlass:
def __eq__(self, other):
# equal to anything that is False
return not other

Better to:
from types import NoneType

x = None
if type( x ) == NoneType:
# true
< code >
else:
# false; do something else.
< more code >

Not necessary. Since None is a guaranteed singleton, the only test you
need to make is "if x is None: ...".

But if you wanted to do extra work unnecessarily, a less unnecessary
amount of extra work would be:

if type(x) == type(None): ...

You don't need to look up the type of None in the types module when you
can easily get it from the type() function.

--
Steven.

Apr 19 '07 #11
Steven Howe a écrit :
(snip)
I've read and found that 'None' comparisons is not always a good idea.
Better to:
from types import NoneType

x = None
if type( x ) == NoneType:
# true
< code >
else:
# false; do something else.
< more code >
Actually, None is garanteed to be a singleton, so the idiomatic way is
to use an identity test:

if x is None:
# code here

But this doesn't answer the OP...

HTH

Apr 19 '07 #12
Alan Isaac <ai****@american.eduwrote:
currently documented behavior:
"objects of different types always compare unequal".
Where is that documented? URL please?
>>1.0 == 1
True
>>type(1.0), type(1)
(<type 'float'>, <type 'int'>)

here, just as an example, are two objects of different types that
compare equal; therefore that "documented behavior" is flat wrong and
needs to be fixed.
Alex
Apr 20 '07 #13
Steven D'Aprano <st***@REMOVE.THIS.cybersource.com.auwrote:
But if you wanted to do extra work unnecessarily, a less unnecessary
amount of extra work would be:

if type(x) == type(None): ...
Of course, like the original poster's proposal, this CAN be faked out
(while the 'is' test cannot, one more weird reason why it's better):
>>class metaWeird(type):
.... def __eq__(self, other): return True
....
>>class Weird: __metaclass__ = metaWeird
....
>>x = Weird()
type(x) == type(None)
True
(warning to all innocent bystanders: don't try this at home, kids!-)
Alex
Apr 20 '07 #14

On Apr 19, 2007, at 11:00 PM, Alex Martelli wrote:
Alan Isaac <ai****@american.eduwrote:
>currently documented behavior:
"objects of different types always compare unequal".

Where is that documented? URL please?
>>>1.0 == 1
True
>>>type(1.0), type(1)
(<type 'float'>, <type 'int'>)
Isn't this an example of numerical comparison (= or !=) versus
object comparison (is or is not). I think the documentation needs
to state that there is a difference between the two types.

Cheers
Tommy
Apr 20 '07 #15
Tommy Grav <tg***@mac.comwrote:
On Apr 19, 2007, at 11:00 PM, Alex Martelli wrote:
Alan Isaac <ai****@american.eduwrote:
currently documented behavior:
"objects of different types always compare unequal".
Where is that documented? URL please?
>>1.0 == 1
True
>>type(1.0), type(1)
(<type 'float'>, <type 'int'>)

Isn't this an example of numerical comparison (= or !=) versus
object comparison (is or is not). I think the documentation needs
to state that there is a difference between the two types.
Operator == (TWO equal signs, not just one: that would be an assignment
instead) can be applied to arbitrary objects, not just numbers, just
like operator 'is'.
>>u'ciao' == 'ciao'
True
>>type(u'ciao'), type('ciao')
(<type 'unicode'>, <type 'str'>)

See, it's not a question of numbers versus other things: the
distinction, rather, is between comparison of the values of objects
(which can perfectly well be equal even for objects of different types)
versus checking object identity (and since an object only has one type
at a time, it can't be "the same object" as one with a different type).
Why do you think the Python docs don't draw the difference between '=='
(equality) and 'is' (identity)?

I'm still interested to know where that erroneous quote from Alan Isaac
comes from, because if it's in Python's docs, it can be fixed.
Alex
Apr 20 '07 #16
En Fri, 20 Apr 2007 11:40:00 -0300, Alex Martelli <al***@mac.comescribió:
I'm still interested to know where that erroneous quote from Alan Isaac
comes from, because if it's in Python's docs, it can be fixed.
It was a partial quote, that's why it appeared to be wrong:

Library reference, 3.3 Comparisons

"Objects of different types, except different numeric types and different
string types, never compare equal; such objects are ordered consistently
but arbitrarily."

Reference Manual, 5.9 Comparisons

"The objects need not have the same type. If both are numbers, they are
converted to a common type. Otherwise, objects of different types always
compare unequal, and are ordered consistently but arbitrarily."

(Apart from the latter not menctioning string types too, looks good to me).

--
Gabriel Genellina
Apr 20 '07 #17
Gabriel Genellina <ga*******@yahoo.com.arwrote:
En Fri, 20 Apr 2007 11:40:00 -0300, Alex Martelli <al***@mac.comescribió:
I'm still interested to know where that erroneous quote from Alan Isaac
comes from, because if it's in Python's docs, it can be fixed.

It was a partial quote, that's why it appeared to be wrong:

Library reference, 3.3 Comparisons

"Objects of different types, except different numeric types and different
string types, never compare equal; such objects are ordered consistently
but arbitrarily."

Reference Manual, 5.9 Comparisons

"The objects need not have the same type. If both are numbers, they are
converted to a common type. Otherwise, objects of different types always
compare unequal, and are ordered consistently but arbitrarily."

(Apart from the latter not menctioning string types too, looks good to me).
Right. However, it might be worth underscoring that this applies to
_built-in_ types, and user-defined types are free to implement different
comparison semantics, although that should be done with care and good
taste; for example, one might have a collection type defining __eq__ to
mean "the same set of items as the other [iterable] operand in any
order", though that might cause weird behavior such as a==b but b!=a
(unfortunately there's no __req__, and __coerce__ is not involved in
comparisons either).

Still, not a major concern -- numbers and strings _do_ exhaust a vast
majority of the use cases for object of different types comparing equal.
Alex
Apr 21 '07 #18
En Fri, 20 Apr 2007 23:48:02 -0300, Alex Martelli <al***@mac.comescribió:
Gabriel Genellina <ga*******@yahoo.com.arwrote:
>Reference Manual, 5.9 Comparisons

"The objects need not have the same type. If both are numbers, they are
converted to a common type. Otherwise, objects of different types always
compare unequal, and are ordered consistently but arbitrarily."

(Apart from the latter not menctioning string types too, looks good to
me).

Right. However, it might be worth underscoring that this applies to
_built-in_ types, and user-defined types are free to implement different
comparison semantics, although that should be done with care and good
taste; for example, one might have a collection type defining __eq__ to
mean "the same set of items as the other [iterable] operand in any
order", though that might cause weird behavior such as a==b but b!=a
(unfortunately there's no __req__, and __coerce__ is not involved in
comparisons either).
BTW, I think that the relationship between __eq__ and __hash__ (a==b =>
hash(a)==hash(b), when hashable) should be more clearly stated too. I
almost got insane some time ago because of a faulty Rational numbers
implementation where hash(1/2)!=hash(2/4) (so dictionaries didn't work at
all).

--
Gabriel Genellina

Apr 21 '07 #19

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

Similar topics

6
by: sam | last post by:
Hi, I got the the following syntax error in comparison: File "/usr/local/work/myparser.py", line 85 if ( (m=self.macro_parser.match (d)) != None ): ^ SyntaxError: invalid syntax How can I...
7
by: rickle | last post by:
I'm trying to compare sun patch levels on a server to those of what sun is recommending. For those that aren't familiar with sun patch numbering here is a quick run down. A patch number shows...
2
by: Dave | last post by:
Hello all, I would like to solicit suggestions on how to most efficiently accomplish something. I have profiled the project I'm working on and have found that calls to fabs() are taking a very...
29
by: Steven D'Aprano | last post by:
Playing around with comparisons of functions (don't ask), I discovered an interesting bit of unintuitive behaviour: >>> (lambda y: y) < (lambda y: y) False Do the comparison again and things...
2
by: Anabella Giseour | last post by:
Is there any tool (preferably free) for database structure comparison. This would be very useful for application maintainance. I'm having problems with upgrades to newer version of application. ...
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...
14
by: Spoon | last post by:
Hello, I've come across the following comparison function used as the 4th parameter in qsort() int cmp(const void *px, const void *py) { const double *x = px, *y = py; return (*x > *y) -...
2
by: r.grimm | last post by:
Hello, I'm a little confused about None in comparison. 3086100672L 134541104 True I thought, the id of the object is the last comparison criterion. Therefore, None must be greater then 1....
11
by: Thomas Heller | last post by:
Does Python 3 have no way anymore to sort with a comparison function? Both .sort() and sorted() seem to accept only 'key' and 'reverse' arguments, the 'cmp' argument seems to be gone. Can that...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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
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
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...

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.