Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #16  
Old August 1st, 2003, 07:18 AM
sacrilege sacrilege is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2002
Location: Norwich, UK
Posts: 53 sacrilege User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 9 h 1 m 53 sec
Reputation Power: 6
The python docs quickly clear this up:

Quote:
The operators <, >, ==, >=, <=, and != compare the values of two objects. The objects need not have the same type.

Quote:
The operators is and is not test for object identity: x is y is true if and only if x and y are the same object. x is not y yields the inverse truth value.

Reply With Quote
  #17  
Old August 1st, 2003, 10:59 AM
SolarBear's Avatar
SolarBear SolarBear is offline
onCsdfeu
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Location: Canada
Posts: 100 SolarBear User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 7 m 43 sec
Reputation Power: 6
Send a message via ICQ to SolarBear Send a message via MSN to SolarBear
So, if I get it correctly,
Code:
x = 1
y = 1
if x is y:
    print 'True'
else:
    print 'False'

would return false, since x and y have the same value but are not the same object ? (i.e. id(x) != id(y) )

Reply With Quote
  #18  
Old August 1st, 2003, 11:10 AM
sacrilege sacrilege is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2002
Location: Norwich, UK
Posts: 53 sacrilege User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 9 h 1 m 53 sec
Reputation Power: 6
No, it would return print 'True' because they both automatically reference 1, which is an integer object.

If you had:
Code:
x = 'test string'
y = 'test string'
if x is y: print 'true'
else: print 'false'

then it would print 'false' because those are two separate string objects.

If you had:
Code:
x = 1.0
y = 1.0
if x is y: print 'true'
else: print 'false'

then it would print 'False'. I'm not entirely sure on the logic behind integers of the same value referencing the same object but floating point numbers not, but there you go. That's the way it is.

The general idea you should get from this is that for most comparisons you'll want to use ==.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Python 2.3


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
Stay green...Green IT