P
Paul Rubin
Steven D'Aprano said:L[0].append(5) # mutate L, in some reasonable sense of "mutate"
You haven't mutated the tuple called "L". You've mutated its internal
components, which are lists. If you wanted them to also be immutable, you
should have used tuples
Obviously you are correct in one reasonable sense of "mutate". In
another sense, if L is immutable and you evaluate a purely computational
function on it two different times, you should get the same result both
times; for example, if you pickle L twice you should get two identical
pickles. That's what I think of "immutable" as meaning, and obviously
isn't the case with this L.
The real point I was trying to make is that there are two definitions of
"constant" that we care about: immutability and resistance to name re-
binding. (Unbindability?) Python already gives us all the tools we need
for immutability (modulo the usual disclaimers about "we're all adult
here", "private attributes are private by convention", etc.). What Python
doesn't have is any way of prohibiting name rebinding short of creating a
new reserved word like None.
I think I'd also want to be able to make instance attributes
un-rebindable. E.g.
class Foo(object):
@const
def wibble(self, a): ...
it shouldn't be possible to redefine x.wibble if x is a Foo. That
should allow for compile-time binding in a lot of practical code.