'isimmutable' and 'ImmutableNester'
Frank-Rene Schäfer
fschaef at gmail.com
Tue Nov 12 04:12:22 EST 2013
(1) hash()-ability != immutability (!)
Proof:
class X:
def __hash__(self): return 0
def pseudo_isimmutable(this):
try:
hash(this)
return True
except TypeError:
return False
shapeshifter = (1, 2, X())
print pseudo_isimmutable(shapeshifter)
shapeshifter[2].changed = 4711
(2) The intended scenario is not described by a fragment such as:
if isimmutable(obj): x = obj
else: x = copy.copy(obj)
function_that_might_modify(x)
But instead, a more characteristic scenario is
assert isimmutable(obj)
# What happens behind the curtain may rely on referencing
things_behind_the_curtain(obj)
Or,
def let_me_know():
obj = get_what_is_wanted()
assert isimmutable(obj)
# The caller may do with it what he wants without risking consistency
return obj
where lots of copying
2013/11/11 <random832 at fastmail.us>:
>> A built-in function 'isimmutable()' shall tell efficiently whether the
>> object
>> of concern is mutable or not.
>
> What's the benefit over attempting to hash() the object?
>
> copy.deepcopy already has special case for int, string, and tuples
> (including tuples that do and do not have mutable members) - could what
> you need be accomplished by overriding __copy__ and __deepcopy__ in your
> custom class to return itself if it is immutable?
2013/11/11 <random832 at fastmail.us>:
>> A built-in function 'isimmutable()' shall tell efficiently whether the
>> object
>> of concern is mutable or not.
>
> What's the benefit over attempting to hash() the object?
>
> copy.deepcopy already has special case for int, string, and tuples
> (including tuples that do and do not have mutable members) - could what
> you need be accomplished by overriding __copy__ and __deepcopy__ in your
> custom class to return itself if it is immutable?
More information about the Python-list
mailing list