From: D-Man <ds...@ri...> - 2001-03-27 20:53:07
|
On Tue, Mar 27, 2001 at 02:37:15PM -0600, Robert W. Bill wrote: | | I've ran into a bit of confusion with the id() of characters in strings, not characters. In (CP|J)ython a character is simply a string with a length of 1. In, say, C | C++ | Java a char is different than a char* , g_string (glib) | char* , std::string (STL) | java.lang.String | Jython compared with Python. Here's something similar to the code | that started the confusion: | | ---------------------------------------------------------- | S = "abc" | stringtest = S[1] | | def test(obj1, obj2): | if ( id(obj1) != id(obj2) ): | return 0 | | assert(test(stringtest, S[1])), "String test failed" | ---------------------------------------------------------- | | In CPython, all passes, but in Jython, there's always an AE: | String test failed. Is this expected? unavoidable? I recall hearing that CPython "interns" short strings. From my understanding, this basically means that it caches the string object and uses it for every time that string is used. There is no guarantee that any particular string will be interned, it could be an entirely new object. It seems that Jython doesn't intern the strings, so they are new objects. They should compare as being equal, even though they are different objects. This is just an implementation detail, and CPython optimizes it more than Jython. -D |