Consistency in Python

Hendrik van Rooyen mail at microcorp.co.za
Sat Aug 26 04:52:00 EDT 2006


"Diez B. Roggisch" <deets at nospam.web.de> wrote, oder schrieb, of het geskryf:

| 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

I understand that dicts are actually a bit special - Its very simple to add
something to a dict - you just do it - but the in the other cases what I have in
mind is more in line with some of what Paul Boddie wrote...

- Hendrik







More information about the Python-list mailing list