hello guys
i just ran into this when comparing negative numbers, they start
returning False from -6 down, but only when comparing with 'is'
>>m = -5 a = -5 m is a
True
>>m = -6 a = -6 m is a
False
>>m == a
True
i read that 'is' compares if they are really the same object, but i
don't that's it because then why does -5 return True?
of course i could only use == to compare, but still, what am i missing here?
thanks in advance
ariel 6 5962
On Aug 14, 1:18*pm, ariel ledesma <fay...@gmail.comwrote:
hello guys
i just ran into this when comparing negative numbers, they start
returning False from -6 down, but only when comparing with 'is'
*>>m = -5
*>>a = -5
*>>m is a
True
*>>m = -6
*>>a = -6
*>>m is a
False
*>>m == a
True
i read that 'is' compares if they are really the same object, but i
don't that's it because then why does -5 return True?
of course i could only use == to compare, but still, what am i missing here?
thanks in advance
ariel
strange, I get the same thing.
From the docs
The operators is and is not test for object identity: x is y is true
if and only if x and y are the same object. x is not y yields the
inverse truth value.
so:
>a = b = -6 a is b
True
It is odd behaviour, but I would stick to '==' when comparing numeric
values
On Thu, 14 Aug 2008 17:18:55 -0300, ariel ledesma wrote:
hello guys
i just ran into this when comparing negative numbers, they start
returning False from -6 down, but only when comparing with 'is'
>>m = -5
>>a = -5
>>m is a
True
>>m = -6
>>a = -6
>>m is a
False
>>m == a
True
i read that 'is' compares if they are really the same object, but i
don't that's it because then why does -5 return True? of course i could
only use == to compare, but still, what am i missing here? thanks in
advance
They also return False for positive numbers 256. Try this:
>>print [x for x in range(-10,260) if x is not x+1-1]
[-10, -9, -8, -7, -6, 257, 258, 259]
There is a good explanation for this: the "is" operator checks for object
identity, not equality. Basically "a is m" asks, does the variable name
"a" reference the same memory location as the variable name "m"?
For integers in the range of -5<=x<=256, Python pre-caches all of these
values, and whenever a variable takes on one of those values, it uses the
cached value. This is an optimization: it prevents the need to allocate
a new Python object for these very very common small integer values.
For integer literals outside this range, a new Python object gets created
when they are assigned to variables, so a=500 followed by m=500 will
create new objects.
The "is" operator just shows the effect of this caching. It's
unimportant for real code since you never care whether two numeric
variables refer to the same object (only important for complex data
structures where their storage may overlap)... only whether they are
equal or not.
Dan Lenski
(PS- The small integer pre-caching is described somewhere in the C API
docs.)
On Aug 15, 8:56*am, Dan Lenski <dlen...@gmail.comwrote:
On Thu, 14 Aug 2008 17:18:55 -0300, ariel ledesma wrote:
hello guys
i just ran into this when comparing negative numbers, they start
returning False from -6 down, but only when comparing with 'is'
*>>m = -5
*>>a = -5
*>>m is a
True
*>>m = -6
*>>a = -6
*>>m is a
False
*>>m == a
True
i read that 'is' compares if they are really the same object, but i
don't that's it because then why does -5 return True? of course i could
only use == to compare, but still, what am i missing here? thanks in
advance
They also return False for positive numbers 256. *Try this:
*>>print [x for x in range(-10,260) if x is not x+1-1]
[-10, -9, -8, -7, -6, 257, 258, 259]
There is a good explanation for this: the "is" operator checks for object
identity, not equality. *Basically "a is m" asks, does the variable name
"a" reference the same memory location as the variable name "m"?
For integers in the range of -5<=x<=256, Python pre-caches all of these
values, and whenever a variable takes on one of those values, it uses the
cached value. *This is an optimization: it prevents the need to allocate
a new Python object for these very very common small integer values.
For integer literals outside this range, a new Python object gets created
when they are assigned to variables, so a=500 followed by m=500 will
create new objects.
The "is" operator just shows the effect of this caching. *It's
unimportant for real code since you never care whether two numeric
variables refer to the same object (only important for complex data
structures where their storage may overlap)... only whether they are
equal or not.
Dan Lenski
(PS- The small integer pre-caching is described somewhere in the C API
docs.)
It would be nice to put together a really canonical case of the use of
the 'is' comparison. FTSOA for the sake of argument, when do you use
it? Why is it even in the language?
If you're iterating over a list,
flagA= object()
flagB= object()
flagC= -10
listA= [ objectA, objectB, flagA, objectC, flagB, -10, objectD ]
flags= [ listA, listB, listC ]
for iA in listA:
for flag in flags:
if iA is flag:
handle( iA )
This case actually misses handleC(). The solution is that the
function that is returning '-10' cannot return -10, it has to return
flagC. This can cause difficulties in cases when you're doing
operations on flags. Worse, if flagC is nested somewhere, say
moduleA.classB.flagC, you still have to work with that, not its value.
castironpi wrote:
This case actually misses handleC(). The solution is that the
function that is returning '-10' cannot return -10, it has to return
flagC. This can cause difficulties in cases when you're doing
operations on flags. Worse, if flagC is nested somewhere, say
moduleA.classB.flagC, you still have to work with that, not its value.
do you *ever* make any sense at all?
</F>
castironpi wrote:
It would be nice to put together a really canonical case of the use of
the 'is' comparison. FTSOA for the sake of argument, when do you use
it? Why is it even in the language?
My poster child use case is in a MUDD game. For instance, the player
represented by `this_player` has picked up the yoghurt. We notify the
other players using code that boils down to:
for person in this_room.inhabitants:
if person is not this_player:
person.notify ('%s has picked up the %s.'
% (this_player.name, 'yoghurt'))
The `is` test avoids telling this_player something he already knows.
Perhaps the code could be written to make an equality test work, but then
again, perhaps the game could have a much more interesting use for equality
between persons.
Mel.
Mel wrote:
castironpi wrote:
>It would be nice to put together a really canonical case of the use of the 'is' comparison. FTSOA for the sake of argument, when do you use it? Why is it even in the language?
My poster child use case is in a MUDD game. For instance, the player
represented by `this_player` has picked up the yoghurt. We notify the
other players using code that boils down to:
for person in this_room.inhabitants:
if person is not this_player:
person.notify ('%s has picked up the %s.'
% (this_player.name, 'yoghurt'))
The `is` test avoids telling this_player something he already knows.
Perhaps the code could be written to make an equality test work, but then
again, perhaps the game could have a much more interesting use for equality
between persons.
Excellent example. There are three uses for 'is'.
1. Minor optimization of comparison with None, True, False.
2. Testing the implementation: 'a=1;b=1;a is b' *should* be True, while
'a=257;b=257;a is b' *should* be False. The CPython test suite has
tests like this.
3. Comparision of user class objects where identify is important.
Objects representing people is certainly such a case ;-).
tjr This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: pj |
last post by:
Why does M$ Query Analyzer display all numbers as positive, no matter
whether they are truly positive or negative ?
I am having to cast each column to varchar to find out if there are
any...
|
by: Subrahmanyam Arya |
last post by:
Hi Folks ,
I am trying to solve the problem of reading the numbers correctly
from a serial line into an intel pentium processor machine .
I am reading 1 byte and 2byte data both positive...
|
by: John |
last post by:
Hi,
I encountered a strange problem while debugging C code for a
Windows-based application in LabWindows CVI V5.5, which led me to
write the test code below. I tried this code with a different...
|
by: jaks.maths |
last post by:
How to convert negative integer to hexadecimal or octal number?
Ex: -568
What is the equivalent hexadecimal and octal number??
|
by: drtimhill |
last post by:
I'm just starting out on Python, and am stumped by what appears an oddity in the way negative indices are handled.
For example, to get the last character in a string, I can enter "x". To get the...
|
by: Frederick Gotham |
last post by:
I have a general idea about how negative number systems work, but I'd
appreciate some clarification if anyone would be willing to help me.
Let's assume we're working with an 8-Bit signed integer,...
|
by: Hallvard B Furuseth |
last post by:
As far as I can tell, (x & -1) is nonzero if the integer x is
negative zero. So for signed types, x == 0 does not guarantee
(x & foo) == 0. Is that right? (Not that I expect to ever
encounter a...
|
by: Casey |
last post by:
Is there an easy way to use getopt and still allow negative numbers as
args? I can easily write a workaround (pre-process the tail end of
the arguments, stripping off any non-options including...
|
by: ariel ledesma |
last post by:
Christian Heimes wrote:
i see now, so i guess that's also why id() returns the same address for
them as well...
i'll dive into the implementation file, it may be a bit out of my league
but i'll...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
| |