kj <no.em...@please.post> writes: > I want to implement a test t() that will return True if its two > arguments are "completely" different. By this I mean that they > don't share any "non-atomic" component. E.g., if > > a = [0, 1] > b = [0, 1] > c = [2, 3] > d = [2, 3] > > A = (a, c, 0) > B = (a, d, 1) > C = (b, d, 0) > > The desired test t() would yield: > > t(A, B) -> False (A and B share the mutable component a) > t(A, C) -> True (a =!= c, b =!= d, and 0 is not mutable) > t(B, C) -> False (B and C share the mutable component d) > > (=!= is shorthand with "is not identical to".) > > It would facilitate the implementation of t() to have a simple test > for mutability. Is there one? > > Thanks! > > ~kj
I think defining mutability is subject to opinion, but here is a first approximation. def mutable(obj): return obj.__hash__ is None or type(obj).__hash__ == object.__hash__ def t(l1, l2): return not any(mutable(x) and x is y for x, y in zip(l1, l2)) a = [0, 1] b = [0, 1] c = [2, 3] d = [2, 3] A = (a, c, 0) B = (a, d, 1) C = (b, d, 0) >>> t(A, B) False >>> t(A, C) True >>> t(B, C) False -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list