By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,907 Members | 1,832 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,907 IT Pros & Developers. It's quick & easy.

How to test if an object IS another object?

P: n/a
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
Share this Question
Share on Google+
10 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.