how to test for atomicity/mutability/hashability?

Discussion in 'Python' started by kj, Oct 7, 2010.

  1. kj

    kj Guest

    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
     
    kj, Oct 7, 2010
    #1
    1. Advertising

  2. kj

    Chris Rebert Guest

    On Thu, Oct 7, 2010 at 12:13 PM, kj <> wrote:
    <snip>
    > It would facilitate the implementation of t() to have a simple test
    > for mutability.  Is there one?


    Non-default hashability is an approximate heuristic:

    def is_immutable(x):
    try:
    hash(x)
    except TypeError:
    return False
    else:
    klass = type(x)
    return klass is object or klass.__hash__ is not object.__hash__

    Cheers,
    Chris
    --
    http://blog.rebertia.com
     
    Chris Rebert, Oct 7, 2010
    #2
    1. Advertising

  3. kj <> 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
     
    Arnaud Delobelle, Oct 7, 2010
    #3
  4. kj

    Chris Rebert Guest

    On Thu, Oct 7, 2010 at 1:46 PM, Arnaud Delobelle <> wrote:
    > kj <> 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__


    Corner case (I think):
    >>> a = object()
    >>> a.b = "c"

    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    AttributeError: 'object' object has no attribute 'b'
    >>> mutable(a)

    True

    Cheers,
    Chris
     
    Chris Rebert, Oct 7, 2010
    #4
  5. Chris Rebert <> writes:

    > On Thu, Oct 7, 2010 at 1:46 PM, Arnaud Delobelle <> wrote:

    [...]
    >> 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__

    >
    > Corner case (I think):
    >>>> a = object()
    >>>> a.b = "c"

    > Traceback (most recent call last):
    > File "<stdin>", line 1, in <module>
    > AttributeError: 'object' object has no attribute 'b'
    >>>> mutable(a)

    > True


    Ah true. There was also the problem that e.g. mutable(tuple) was False.

    How about this second approximation:

    def mutable(obj):
    h = type(obj).__hash__
    return not h or h is object.__hash__ and type(obj) is not object

    --
    Arnaud
     
    Arnaud Delobelle, Oct 7, 2010
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mirko Zeibig

    Atomicity of signal handlers

    Mirko Zeibig, Aug 7, 2003, in forum: Python
    Replies:
    0
    Views:
    307
    Mirko Zeibig
    Aug 7, 2003
  2. =?iso-8859-1?Q?Fran=E7ois?= Pinard

    Hashability of classes, old and new

    =?iso-8859-1?Q?Fran=E7ois?= Pinard, Feb 16, 2004, in forum: Python
    Replies:
    1
    Views:
    279
    Josiah Carlson
    Feb 16, 2004
  3. Chris S.

    Hashability?

    Chris S., Jul 19, 2004, in forum: Python
    Replies:
    5
    Views:
    423
    Chris S.
    Jul 19, 2004
  4. James Stroud

    hashability

    James Stroud, Aug 12, 2009, in forum: Python
    Replies:
    20
    Views:
    675
    Carl Banks
    Aug 12, 2009
  5. Bob Grommes

    Hashability questions

    Bob Grommes, May 13, 2012, in forum: Python
    Replies:
    2
    Views:
    228
    Bob Grommes
    May 15, 2012
Loading...

Share This Page