Re: how to test for atomicity/mutability/hashability?

2010-10-07 Thread Chris Rebert
On Thu, Oct 7, 2010 at 12:13 PM, kj no.em...@please.post 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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to test for atomicity/mutability/hashability?

2010-10-07 Thread Arnaud Delobelle
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


Re: how to test for atomicity/mutability/hashability?

2010-10-07 Thread Chris Rebert
On Thu, Oct 7, 2010 at 1:46 PM, Arnaud Delobelle arno...@gmail.com wrote:
 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__

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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to test for atomicity/mutability/hashability?

2010-10-07 Thread Arnaud Delobelle
Chris Rebert c...@rebertia.com writes:

 On Thu, Oct 7, 2010 at 1:46 PM, Arnaud Delobelle arno...@gmail.com 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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to test for atomicity/mutability/hashability?

2010-10-07 Thread Christian Heimes
Am 07.10.2010 22:02, schrieb Chris Rebert:
 On Thu, Oct 7, 2010 at 12:13 PM, kj no.em...@please.post 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:

Except that every user defined class is hashable and mutable by default ;)

 class Example(object):
... pass
...
 example = Example()
 hash(example)
139810551284624
 example.__dict__
{}
 example.egg = spam
 example.__dict__
{'egg': 'spam'}

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to test for atomicity/mutability/hashability?

2010-10-07 Thread Chris Rebert
On Thu, Oct 7, 2010 at 2:28 PM, Christian Heimes li...@cheimes.de wrote:
 Am 07.10.2010 22:02, schrieb Chris Rebert:
 On Thu, Oct 7, 2010 at 12:13 PM, kj no.em...@please.post 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:

 Except that every user defined class is hashable and mutable by default ;)

 class Example(object):
 ...     pass
 ...
 example = Example()
 hash(example)
 139810551284624
 example.__dict__
 {}
 example.egg = spam
 example.__dict__
 {'egg': 'spam'}

Hence exactly why I said *non-default* hashability (i.e. the object is
hashable, but does not use the default implementation which you point
out exists):

 is_immutable(Example())
False

Not that the heuristic I suggested is infallible or anything though.

Cheers,
Chris
--
http://blog.rebertia.com
-- 
http://mail.python.org/mailman/listinfo/python-list