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

How to test if an object IS another object?

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

Jul 19 '05 #1
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
Jul 19 '05 #2
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

Jul 19 '05 #3
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 ;-).
Jul 19 '05 #4
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


Jul 19 '05 #5
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.
Jul 19 '05 #6
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

Jul 19 '05 #7
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

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.
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
Jul 19 '05 #8
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...
Jul 19 '05 #9
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

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? Yet applying the is operator yields True.

Thanks,
-Dan

Jul 19 '05 #10
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('@')])"
Jul 19 '05 #11

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

Similar topics

11
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...
9
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) {
0
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;
12
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
6
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...
176
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...
9
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+...
60
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...
4
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'...
2
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...
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
BarryA
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...
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
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
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...
0
tracyyun
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...

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.