Question about consistency in python language

Kay Schluehr kay.schluehr at gmx.net
Fri Sep 9 02:37:16 EDT 2005


lechequier at gmail.com wrote:

> Let's say I define a list of pairs as follows:
> >>l = [('d', 3), ('a', 2), ('b', 1)]
>
> Can anyone explain why this does not work?
> >>h = {}.update(l)
>
> and instead I have to go:
> >>h = {}
> >>h.update(l)
> to initialize a dictionary with the given list of pairs?
>
> when an analagous operation on strings works fine:
> >>s = "".join(["d","o","g"])
>
> Seems inconsistent.

If you define

>>> sep = ""
>>> sep.join(["d","o","g"])
"dog"
>>> sep
''

sep is preserved and a new "dog" string is generated. Since sep is
immutable there is no way to manipulate it inplace.

On the other hand there exists no sorted() method for tuples or lists
like join() for strings but it is implemented as a function in Python24
that returns a new sorted container. I consider this as an
inconsistency across builtin types. Consistent would be following usage
pattern:

>>> l = [1,3,2]
>>> l.sorted()
[1,2,3]             # new sorted list
>>> l.sort()        # sort list inplace
>>> l.appended(4)   # new extended list
[1,2,3,4]
>>> l.append(4)     # appends an element to the same list
>>> l
[1,2,3,4]

Preserving the naming convention we would have

>>> "".joined(["d","o","g"])
"dog"

Kay




More information about the Python-list mailing list