curly-brace-aphobic?

C.Laurence Gonsalves clgonsal at keeshah.penguinpowered.com
Mon Jan 29 03:01:55 EST 2001


On Sun, 28 Jan 2001 22:04:38 -0600, Grant Griffin
<not.this at seebelow.org> wrote:
>Hi Gang,
>
>I'm sure this has been asked a thousand times, but I haven't seen it
>asked lately (or ever <wink>).
>
>Why does Python use square braces ([]) instead of curly braces ({})
>when accessing dictionaries?
>
>This strange inconsistency in Python seems strangely inconsistent with
>its otherwise very consistent application of consistency: since we use

I think there are many that would argue that using a different notation
would be far *more* inconsistent. 

x[y] gets the thing that x maps y to. In Python, a sequence is a map of
integers to values, while a dictionary is a map of arbitrary keys to
values. Why use a different notation for what's really the same sort of
thing?

Actually, the thing that's always bugged me is that Python makes a
distinction between subscripting (__getattr__) and function calls
(__call__). Mathematically speaking, a dictionary is really just a
function that maps from 'keys' to 'values'.

It always seems silly to me that given:

    m = {'one': 1, 'two':2}
    l = ['two', 'one']

one can't write:

    map(m, l)

Instead one has to write:

    map(m.get, l)

or (depending on your level of paranoia):

    map(lambda k,m=m:m[k], l)

>Anyway, although curly braces are perhaps slightly harder to type than
>square braces, this is one example (maybe the only one) where I find
>Perl code more readable: I appreciate the mnemonic effect curly braces
>have in reminding me that the thing being accessed is a dictionary*.

I'm not sure why you need to be reminded whether something is a sequence
or a dictionary.

-- 
  C. Laurence Gonsalves                "Any sufficiently advanced
  clgonsal at kami.com                     technology is indistinguishable
  http://cryogen.com/clgonsal/          from magic." -- Arthur C. Clarke



More information about the Python-list mailing list