Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #59165
| References | <CAB6+5b_w+kBT=EDqd9wRO80am+Wp2DgrEqSpVEPWkcTAVmYQtQ@mail.gmail.com> <1384206048.30461.46091021.634F0FCA@webmail.messagingengine.com> |
|---|---|
| Date | 2013-11-12 10:12 +0100 |
| Subject | Re: 'isimmutable' and 'ImmutableNester' |
| From | Frank-Rene Schäfer <fschaef@gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.2439.1384247550.18130.python-list@python.org> (permalink) |
(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@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@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?
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: 'isimmutable' and 'ImmutableNester' Frank-Rene Schäfer <fschaef@gmail.com> - 2013-11-12 10:12 +0100
csiph-web