Message5173
This has to do with string interning.
When executed in a file we discover all the occurrences of the same
string and use the same object for them (the same as CPython), but when
running in interactive mode we use new string objects for all strings
that are in different compilation units. CPython does it this way to.
The reason this is not working is because 'is' (and 'is not') is an
identity equals comparison, it returns true if the objects are the same,
and should not be used to compare strings. For string comparison '=='
and '!=' should be used.
The reason the example works in CPython is that the strings are
"identifier looking strings", i.e. strings that are valid as
identifiers, and CPython interns all such strings since it improves
performance there. Jython does not intern strings like that.
If you instead try this code (again in interactive mode) it will behave
the same in CPython as in Jython:
>>> def t(v):
... if v is not 'this string object': return 'e'
... return 't'
...
>>> t('this string object')
'e' |
|
Date |
User |
Action |
Args |
2009-09-17 06:14:17 | thobes | set | messageid: <1253168057.69.0.82905324461.issue1471@psf.upfronthosting.co.za> |
2009-09-17 06:14:17 | thobes | set | recipients:
+ thobes, fwierzbicki, asnama |
2009-09-17 06:14:17 | thobes | link | issue1471 messages |
2009-09-17 06:14:17 | thobes | create | |
|