From: Brian Q. <br...@sw...> - 2001-12-18 19:18:28
|
Clark wrote: > Perhaps you are right; although I 'd rather consider > the Python identity function to be not compliant since > it does not distinguish between the first "1" that I > type in and subsequent times that I type in "1". To me, > these are not identical, but they are equal. But the YAML definition is not that flexible: "In most programming languages, there are two manners in which variables can be equivalent. The first is by reference, where the two variables refer to the same memory address. We call this equivalence relation identity." > Yes. This is the intention. > > a = (1,2) > b = a > > is different from > > a = (1,2) > b = (1,2) > > since the fomer can use aliases, while the latter shouldn't. My point is that serializing built-in Python immutable types according to identity is going to lead to some odd behavior because Python has no reason to provide any clear semantics for those types. Here is another example: >>> import copy >>> a = [1,2] >>> b = a >>> a is b 1 >>> b = copy.copy(a) >>> a is b 0 >>> a = (1,2) >>> b = a >>> a is b 1 >>> b = copy.copy(a) >>> a is b 1 So imagine that I serialize: >>> a = (1,2) >>> b = copy.copy(a) >>> serialize([a,b]) - - &tuple - 1 - 2 - *tuple Note that the second tuple is aliases even though the user specifically asked for a copy (not that any Python programmer ever would)! My intuition is that serialized Python immutable types should never share identity unless some piece of meta information is present (maybe through yaml.copy()). Or maybe that is too conservative... Cheers, Brian |