Message8343

Author zyasoft
Recipients pjac, zyasoft
Date 2014-05-07.19:20:28
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1399490429.35.0.184549751762.issue2137@psf.upfronthosting.co.za>
In-reply-to
Content
So this is a question of whether string literals like "a" should be automatically interned or not.

You can guarantee this with the intern() builtin function. So using this function is guaranteed to work on any Python implementation, regardless of how the string is constructed, literal or not:

intern("a") is intern("a")

My first inclination is to agree with the submitter and say "implementation dependent" and not to support such automatic interning of string literals, even if it occasionally will accidentally work or has worked in the past.

Consider working with ints:

>>> 42 is 42

For Jython, this is True because of Jython's internal caching of ints in the range [-100, 900]. So on Jython, asking if 60000 is 60000 will return False, for example.

CPython takes a different approach, and it's somewhat related to reference counting and probably arenas. But we can investigate a bit further:

>>> a = 60000; b = 60000; a is b # True

but

>>> a = 60000
>>> b = 60000
>>> a is b  # False

I did write a test program to see what CPython's cached range actually is - it's range(-5, 257)

for i in range(-1000, 1000):
    a = i
    b = i * 2 / 2  # Need to do a trivial calculation to avoid object assignment
    print i, a is b
History
Date User Action Args
2014-05-07 19:20:29zyasoftsetmessageid: <1399490429.35.0.184549751762.issue2137@psf.upfronthosting.co.za>
2014-05-07 19:20:29zyasoftsetrecipients: + zyasoft, pjac
2014-05-07 19:20:29zyasoftlinkissue2137 messages
2014-05-07 19:20:28zyasoftcreate