How to test if an object IS another object?

D

dan.eloff

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
 
B

Bruno Desthuilliers

(e-mail address removed) 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
 
E

eloff777

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
 
W

wittempj

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.
 
E

eloff777

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 True
foo is bar
False

Tuples (which are immutable) also appear to be reused
True

Thanks for your help, I know how to solve the problem now.
-Dan
 
B

Bruno Desthuilliers

(e-mail address removed) 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.




I immediately thought of is, and tested it in the console, but it
didn't work quite like I expected:



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:
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.
 
G

Grant Edwards

I immediately thought of is, and tested it in the console, but it
didn't work quite like I expected:

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?
 
G

Grant Edwards

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.
 
R

Roose

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top