Jason Baker // Just another random geek. Visit my homepage at http://jason-baker.com
Follow me on twitter: http://twitter.com/jasonsupdates
Whenever I tell someone I'm working on a persistent data structure
library for Python, I almost always get the same response:
"Persistent means that something is stored across process boundaries.
I think you mean to say immutable data structures." In fairness, this
response is understandable. But it's not quite right. I'm going to
address both of the points in the above statement.
Did you know that letter can mean a note you send through the mail or a thing you use to make up words? Did you know that programming can mean writing computer programs or mathematical optimization? I'm sure if I sat down long enough, I could think of more examples. The point is that the language we speak is full of ambiguities and whatnot.
>>> a = [1,2,3] >>> b = a >>> b.append(4) >>> a [1, 2, 3, 4]Tuples, however, are a different matter:
>>> a = (1,2,3) >>> b = a + (4,) >>> b (1, 2, 3, 4) >>> a (1, 2, 3)Ah, but here's where the second part of this post comes in.
In object-oriented and functional programming, an immutable object is
an object whose state cannot be modified after it is created.
Persistent data structure
In computing, a persistent data structure is a data structure which
always preserves the previous version of itself when it is modified;
such data structures are effectively immutable, as their operations
do not (visibly) update the structure in-place, but instead always
yield a new updated structure.
These definitions have a lot of common ground, but there is some
difference between them. Let's consider a data structure that is
immutable but not persistent. Prepare yourself. This will be
complex. Ready?
>>> 1 1Has your mind been blown yet? In this sense, the number 1 is definitely immutable. You can't change
>>> from copy import copy >>> a = 1 >>> a is copy(a) True