having both dynamic and static variables
Steven D'Aprano
steve+comp.lang.python at pearwood.info
Mon Mar 7 20:25:44 EST 2011
On Mon, 07 Mar 2011 13:20:39 -0800, Paul Rubin wrote:
> Steven D'Aprano <steve+comp.lang.python at pearwood.info> writes:
>> but I call that a feature, not a bug. If you want an immutable
>> constant, use a tuple, not a list.
>
> Nope:
>
> L = ([1,2],[3,4]) # tuple
> 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 :)
It all depends on what sense of mutate you consider "reasonable", but
once you include any mutable object in a compound object, the whole
object becomes mutable in *some sense*. If you care about that sense --
and you may not -- then you have to ensure that each component that you
care about immutability is immutable.
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.
--
Steven
More information about the Python-list
mailing list