[Python-Dev] Re: lists v. tuples
Glyph Lefkowitz
glyph@twistedmatrix.com
Sat, 15 Mar 2003 22:19:30 -0600
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
On Saturday, March 15, 2003, at 12:35 AM, python-dev-request@python.org
wrote:
> Then I realize that C++ has exactly this feature (it's called "const"),
> and that I find it to be an annoyance far more often than I find it
> handy. And I begin to question.
I have thought about this as well; I think that the problem is that in
C++, you have to declare "const" *everywhere* -- you can't just pass a
mutable data structure and have the "right thing" happen the way it
most obviously should. Arguably when one is mucking about at such a
low level as is common in C++, this is something that you have to be
really careful about, but I still think that it's handled badly in the
syntax.
Imagine for a moment that dictionaries and lists in Python had a
"const" method which would immutabilify them (if that's even a word).
The following example:
>> const char* strrev(const char* torev) {
>> // reverse the string
>> }
>> ...
>> char* x = "1234";
>> char* y = (char*) strrev((const char*)x); // tell me I've been bad,
>> g++!
>> ...
As opposed to:
>> def strrev(s):
>> "Please pass me immutable data!"
>> # reverse the string
>> ...
>> x = '1234'
>> y = strrev(x.const()).copy()
>> ...
I think that the latter is far less likely to annoy. Of course, in
this hypothetical example, I can design all kinds of convenient
behavior for these Python mutability operations to have, like 'copy'
always returning a mutable shallow copy of the data structure in
question, and '.const()' making an object immutable and then returning
'self'...
This smells like another unformed PEP I don't have the time to think
about or implement :-(, but I would definitely like to see mutability
guarantees worm their way into the language at some point, too.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (Darwin)
iD8DBQE+c/tWvVGR4uSOE2wRAmeoAJ9tSOYOKTCxcl6Aj6reelmFU8OafwCggcNY
smKTK1+HRCCEC9Pl/mhE4cI=
=cMYA
-----END PGP SIGNATURE-----