From: Ype K. <yk...@xs...> - 2001-03-28 19:09:49
|
Jim, Robert, >On Tue, 27 Mar 2001, Jim Adrig wrote: >> "Robert W. Bill" wrote: >> > >> > Hi all, >> > >> > I've ran into a bit of confusion with the id() of characters in >> > Jython compared with Python. Here's something similar to the code >> > that started the confusion: >> > >> > ---------------------------------------------------------- >> > S = "abc" >> > L = ["a", "b", "c"] >> > LoL = [L, L, L] >> > stringtest = S[1] >> > listtest = L[1] >> > loltest = LoL[1] >> > >> > def test(obj1, obj2): >> > if ( id(obj1) != id(obj2) ): >> > return 0 >> > > > > assert(test(loltest, LoL[1])), "List-of-lists test failed" >> > assert(test(listtest, L[1])), "List test failed" >> > 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? >> Even in CPython the test function should always return a value that evaluates to false. The "test" function returns None in case it does not return 0. Both None and 0 evaluate to boolean false when used after if. You might try this instead: def test(obj1, obj2): return id(obj1) == id(obj2) Also the assert code is a bit strange: 'assert' is a keyword, not a function. Leaving out one pair of brackets gives: assert test(loltest, LoL[1]), "List-of-lists test failed" I don't know what should happen when it is written as above. It might give the standard behaviour, but this seems to be something for advanced language lawyers. To me it looks like a function call followed by a comma and a string. As such you should get a syntax error. However, it is probably parsed as: assert_statement: "assert" expression ["," expression] with (test(loltest, LoL[1])) as the first expression because you saw the "String test failed" error. And then the fun with the string object id's hasn't even started... Regards, Ype |