Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #59165 > unrolled thread
| Started by | Frank-Rene Schäfer <fschaef@gmail.com> |
|---|---|
| First post | 2013-11-12 10:12 +0100 |
| Last post | 2013-11-12 10:12 +0100 |
| Articles | 1 — 1 participant |
Back to article view | Back to comp.lang.python
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Re: 'isimmutable' and 'ImmutableNester' Frank-Rene Schäfer <fschaef@gmail.com> - 2013-11-12 10:12 +0100
| From | Frank-Rene Schäfer <fschaef@gmail.com> |
|---|---|
| Date | 2013-11-12 10:12 +0100 |
| Subject | Re: 'isimmutable' and 'ImmutableNester' |
| Message-ID | <mailman.2439.1384247550.18130.python-list@python.org> |
(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 top | Article view | comp.lang.python
csiph-web