On Wed, Aug 1, 2018 at 9:56 PM, Richard Damon <rich...@damon-family.org> wrote:
>> it says explicitly that numeric keys will use numeric comparison, but
>> no mention is made of strings/bytes etc etc and there's an implication
>> that object identity is used rather than comparison. In python 3.x
>> b'a' is not the same as 'a' either the documentation is lacking some
>> reference to strings/bytes or the warning is wrong. Using the excuse
>> that normal comparison is being used seems a bit feeble. It would
>> clearly improve speed if object identity were to be used, but I
>> suppose that's not the case for other reasons.
>
> One problem with using identity for strings is that strings that have
> the same value may not have the same identity. I believe very short
> strings, like small numbers are cached, so same valued (and typed) short
> strings will have the same identity, but longer strings are not. It
> might work for 'a', but might not for 'employee'.

Strings and numbers MUST be compared by value, not identity. CPython
has a cache for strings used in compilation, but that's about it:

>>> s1 = "spam"
>>> s2, s3 = "sp", "am"
>>> s4 = s2 + s3
>>> s1 is s4, s1 == s4
(False, True)
>>> n1 = 42
>>> n2 = n1 * 3
>>> n3 = n2 / 3
>>> n1 is n3, n1 == n3
(False, True)
>>> n4 = 123456789
>>> n5 = n4 * 3
>>> n6 = n5 // 3
>>> n4 is n6, n4 == n6
(False, True)

The middle example is guaranteed (because n1 is an int, n3 is a
float), but the other two could in theory be cached by a compliant
Python implementation. I don't know of any that do, though.

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list

Reply via email to