|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#16
|
||||
|
||||
|
The python docs quickly clear this up:
Quote:
Quote:
|
|
#17
|
||||
|
||||
|
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) ) |
|
#18
|
|||
|
|||
|
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 ==. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Python Programming > Python 2.3 |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|