If two objects are of equal value you can compare them with ==. What I
want to do is find out if two objects are actually just references to
the same object, how can I do this in Python?
Thanks 10 1111 da*******@gmail.com a écrit : If two objects are of equal value you can compare them with ==. What I want to do is find out if two objects are actually just references to the same object, how can I do this in Python?
The most obvious way (as usual ?):
if obj1 is obj2:
// your code here
Sorry about removing my message, I posted with the wrong google
account, I don't really want my email where those irritating spam bots
can find it. The most obvious way (as usual ?):
if obj1 is obj2: // your code here
I immediately thought of is, and tested it in the console, but it
didn't work quite like I expected:
foo = 3 bar = 3 zoo = foo foo is zoo
Truefoo is bar
Truezoo is bar
True
clearly foo and bar have the same value but they are different objects
aren't they? Yet applying the is operator yields True.
Thanks,
-Dan el******@yahoo.com writes: foo = 3 bar = 3
clearly foo and bar have the same value but they are different objects aren't they?
No, they're the same object. Now try it with 300 instead of 3 ;-).
You can use the id() function to test equality of objects:
martin@ubuntu:~$ python
Python 2.4.1 (#2, Mar 30 2005, 21:51:10)
[GCC 3.3.5 (Debian 1:3.3.5-8ubuntu2)] on linux2
Type "help", "copyright", "credits" or "license" for more information. a = 3 b = 3 id(a)
135585176 id(b)
135585176
el******@yahoo.com a écrit : Sorry about removing my message, I posted with the wrong google account, I don't really want my email where those irritating spam bots can find it.
The most obvious way (as usual ?):
if obj1 is obj2: // your code here
I immediately thought of is, and tested it in the console, but it didn't work quite like I expected:
foo = 3 bar = 3 zoo = foo foo is zoo
True
foo is bar
True
zoo is bar
True
clearly foo and bar have the same value but they are different objects aren't they?
Nope. They are two different names bound to the same integer object. You
may have similar situation with strings: s1 = "toto" s2 = "toto" s1 is s2
True
This is an application of the lightweight pattern. The Python
interpreter reuse the same "value object" to avoid memory clutter. Since
ints and strings are immutable, this is perfectly safe (but yet
confusing when you're not aware of this).
Yet applying the is operator yields True.
Yes. But now you know why !-)
And don't worry, this is quite unlikely that it will cause you any
trouble in real code.
Fascinating. With small strings, it uses the same object, and with
small numbers like 3. With 300 they were different objects (why,
shouldn't they both be ints still?)
Mutable objects functioned differently as you suggested: foo = [] bar = [] foo == bar
Truefoo is bar
False
Tuples (which are immutable) also appear to be reused
foo = () bar = () foo is bar
True
Thanks for your help, I know how to solve the problem now.
-Dan
On 2005-06-12, el******@yahoo.com <el******@yahoo.com> wrote: The most obvious way (as usual ?):
if obj1 is obj2: // your code here I immediately thought of is, and tested it in the console, but it didn't work quite like I expected:
foo = 3 bar = 3 zoo = foo foo is zoo Truefoo is bar Truezoo is bar True
clearly foo and bar have the same value but they are different objects aren't they?
Nope.
Yet applying the is operator yields True.
They're the same object. Why did you expect them not to be?
--
Grant Edwards grante Yow! Am I elected yet?
at
visi.com
On 2005-06-12, el******@yahoo.com <el******@yahoo.com> wrote: Fascinating. With small strings, it uses the same object, and with small numbers like 3. With 300 they were different objects (why,
It's purely an implimentation detail. The small integers get
used a lot, so Python keeps a pre-created set of small integers
handy. It would be a bit, uh, wasteful to pre-create all of
possible integer objects, so "large" integers get created on
the fly without checking to see if there are any existing ones
with the right value. Large integers could get cached and
re-used, but that would be extra overhead with little chance
for benefit.
shouldn't they both be ints still?)
They are.
--
Grant Edwards grante Yow! .. over in west
at Philadelphia a puppy is
visi.com vomiting...
This isn't a good example to test with, since 3 is an immutable object, as
is 300 and all ints.
It's more meaningful if the objects are mutable. Why do you want to test
identity in the first place?
Roose
<el******@yahoo.com> wrote in message
news:11*********************@g47g2000cwa.googlegro ups.com... Sorry about removing my message, I posted with the wrong google account, I don't really want my email where those irritating spam bots can find it.
The most obvious way (as usual ?):
if obj1 is obj2: // your code here
I immediately thought of is, and tested it in the console, but it didn't work quite like I expected:
foo = 3 bar = 3 zoo = foo foo is zoo Truefoo is bar Truezoo is bar True
clearly foo and bar have the same value but they are different objects aren't they? Yet applying the is operator yields True.
Thanks, -Dan el******@yahoo.com wrote: Tuples (which are immutable) also appear to be reused
foo = () bar = () foo is bar
True
Not always:
foo = (1,)
bar = (1,)
foo is bar
=> False
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])" This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Squid Seven |
last post by:
I create a pointer to an item:
CardSession *cardSession;
Then, later, I use new to create an instance of the item and assign it
to that pointer:
cardSession = new CardSession();
In...
|
by: Steve Sargent |
last post by:
Hi:
I'm trying to debug the following code, and it keeps looping on the if
statement:
public static bool operator ==
(OnlineMemberNode first, OnlineMemberNode second)
{
if(first == null)
{
|
by: Josema |
last post by:
Hi, to all.
I have a web control that has only a button for one hand.
for another hand i have a class with one event:
public Class1
{
public event EventHandler Finded;
|
by: Joe Fallon |
last post by:
I would like to know the syntax to check that an Object is a String.
If it was a number test I might use IsNumeric.
But there is no function: IsString (is there?)
--
Joe Fallon
|
by: Darren Linsley |
last post by:
I know this might seem like a dumb question, but how do you test that an
object exists.
Maybe i should explain a little.
I have a variable that points to an object. Now if that oject is...
|
by: nw |
last post by:
Hi,
I previously asked for suggestions on teaching testing in C++. Based
on some of the replies I received I decided that best way to proceed
would be to teach the students how they might write...
|
by: hendedav |
last post by:
Gang,
I am trying to get a regular expression test to work and can't
figure out why. I will give you the code below:
for (var j=0; j<document.getElementById('cmbList').options.length; j+...
|
by: marss |
last post by:
Maybe anyone know good free online JavaScript knowledge test? This not
exactly a system for testing online required - it may be simply list
of questions with variants of answers (I have to prepare...
|
by: Matt |
last post by:
Hello all,
I have just discovered (the long way) that using a RegExp object with
the 'global' flag set produces inconsistent results when its test()
method is executed. I realize that 'global'...
|
by: hcaptech |
last post by:
This is my Test.can you help me ?
1.Which of the following statement about C# varialble is incorrect ?
A.A variable is a computer memory location identified by a unique name
B.A variable's name...
|
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: 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...
|
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,...
|
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,...
|
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: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |