473,583 Members | 3,124 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1122
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******** *************@g 47g2000cwa.goog legroups.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

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

Similar topics

11
23044
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 another function, I want to test if an object is assigned to that
9
2851
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
776
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
49290
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
15432
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 released, how can i test my variable to see if the object it was pointing at, still exists? Please help!!
176
8301
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 their own unit test framework, and then in a lab session see if I can get them to write their own. To give them an example I've created the...
9
2400
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+ +) { if (document.getElementById('cmbList').options.value ==
60
4970
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 tests for learners and I need something to be taken as basis). I was able to find only this (http://www.w3schools.com/js/ js_quiz.asp), but I...
4
3896
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' is not an appropriate modifier for the test() function - test() searches the entire string by default. However, I would expect it to degrade...
2
10015
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 is used to access and read the value stored in it C.A variable is allocated or deallocated in memory during runtime D.A variable can be initialized...
0
7896
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7828
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8186
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8329
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7936
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6583
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5704
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5377
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3847
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.