The unPEP: List-Tuple Unification

Christopher A. Craig com-nospam at ccraig.org
Fri Jul 20 15:00:51 EDT 2001


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Raymond Hettinger <othello at javanet.com> writes:

> The big advantage comes from deprecating tuples, simplifying the
> language, getting rid of (,) and eliminating the need to remember
> which things require tuples, which require lists, and which allow
> both

So then you will have some code that only accepts list which have
their immutable flag set to "0" and some code that will change that
flag transparently.  So we get this behavior

>>> t = [5]
>>> a = t
>>> t += [2]
>>> a == t
1
>>> d = {t: 3}
>>> t += [2]
>>> a == t
0

Despite the fact that I have made no obvious changes to how "t" is
treated.  I think the fact that using a list as a dictionary key
fundamentally changes the behavior of the list is far more confusing
than the current system.

I also fail to see how this makes the tutorial any simpler.  I went
and read section 5.3 of the tutorial (Tuples and Sequences) and
imagined how it would look if you took out all of the tuple/list
distinctions, but added the rules for mutable/immutable changes and I
think it would be a great deal more complex.

Why do this at all?  Why not keep existing tuples and modify existing
code to use anything that supports the sequence interface (maybe
PEP245, maybe just implementing the needed functions)?  Then you could
support not just native lists and tuples, but also user extensions.  

Obviously modification requires mutability, but modifying an immutable
list would fail even with your suggestion.

Use as dictionary keys is the only place I can think of in the
language where immutability is required, and it isn't really required
there. 

>>> class foo:
...   def __hash__(self): return hash(self.n)
...
>>> t = foo()
>>> t.n = 23
>>> d = {t: 5}
>>> t.n = 47
>>> d[t]
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
KeyError: <__main__.foo instance at ff3bc>

So lists _could_ support tp_hash and then they could be used as keys,
but I don't suggest this.  Better to just say that dictionaries
require immutable keys.


> (quick, what does the % formatting operator require?).

tuples, but it would be preferable if they supported any sequence and
recognized when they got a string and expected a single item (so that 
("%s" % "foo") would work)

- -- 
Christopher A. Craig <com-nospam at ccraig.org>
"No one has ever called us untrustworthy" -- Steve Balmer
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: Processed by Mailcrypt

iEYEARECAAYFAjtYf+MACgkQjVztv3T8pzvcYACgsHQkcUWKa/TZWeuBXTiS2156
psAAoMzKbRbsC0+VzivPCcS6G6Ll4w5r
=MZOu
-----END PGP SIGNATURE-----




More information about the Python-list mailing list