Consistency in Python

Diez B. Roggisch deets at nospam.web.de
Fri Aug 25 05:05:53 EDT 2006


Hendrik van Rooyen schrieb:
> Hi,
> 
> for S where S is a Standard Python type:
>     The slice notation S[n] returns either:
>         The n'th element of S, or
>         The value of the dictionary entry whose key is n.
> 
> This is beautiful because as a programmer you don't have to worry what S is...
> (and as an aside - This consistency has already saved my butt when I thought I
> was working with a string that turned out to be a tuple instead - and still
> worked perfectly as expected...)
> 
> Now consider what you have to do to add an element to S...
> (where "add" is used in its meaning of increasing the size of a set, and not 1 +
> 1 = 2)
> 
> There seems to be no common methods such as-
>     "prepend" - for adding something to the beginning
>     "append" - for adding something to the end
>     "insert[j]" - for adding something somewhere in the middle
> 
> Or have I missed something ?

Yes, the nature of collections. dictionaries have no notion of 
"somewhere in the middle". Most of the time they are unordered. If they 
are ordered, they can be ordered by insertion time, key or value value. 
And they always need key, value

So - all these three methods only make sense on sequences which imply a 
key (the index), and are mutable of course - which is why they are 
available on lists only.

Diez



More information about the Python-list mailing list