Perlish dictionary behavior

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Fred Allen

    Perlish dictionary behavior

    Mr. Brewer:

    I fruitlessly tried your "thing counter", as you can see below.
    [color=blue][color=green][color=darkred]
    >>> class AllThingsStartA tZero(dict):[/color][/color][/color]
    .... def getitem (self, key):
    .... return dict.get(self, key, 0)
    ....[color=blue][color=green][color=darkred]
    >>> thingCounts = AllThingsStartA tZero()
    >>> for thing in ('a','b','c','d ','a'):[/color][/color][/color]
    .... thingCounts[thing] += 1
    ....
    Traceback (most recent call last):
    File "<interacti ve input>", line 2, in ?
    KeyError: 'a'[color=blue][color=green][color=darkred]
    >>> thingCounts[/color][/color][/color]
    {}

    Reason tells me it should work for me using...

    PythonWin 2.3.4 (#53, May 25 2004, 21:17:02) [MSC v.1200 32 bit
    (Intel)] on win32.

    ....as it did for you. The runtime interpreter, however, says
    otherwise. Can you see what I've done wrong...I can't?

    With thanks in advance, I am,

    Respectfully yours,

    Fred Allen
  • Chris Liechti

    #2
    Re: Perlish dictionary behavior

    fallen@leveltwo .com (Fred Allen) wrote in
    news:72976037.0 406081556.6d4dd 9e7@posting.goo gle.com:
    [color=blue]
    > Mr. Brewer:
    >
    > I fruitlessly tried your "thing counter", as you can see below.
    >[color=green][color=darkred]
    >>>> class AllThingsStartA tZero(dict):[/color][/color]
    > ... def getitem (self, key):[/color]

    it's __getitem__ note the underlines.
    [color=blue]
    > ... return dict.get(self, key, 0)
    > ...[color=green][color=darkred]
    >>>> thingCounts = AllThingsStartA tZero()
    >>>> for thing in ('a','b','c','d ','a'):[/color][/color]
    > ... thingCounts[thing] += 1
    > ...
    > Traceback (most recent call last):
    > File "<interacti ve input>", line 2, in ?
    > KeyError: 'a'[color=green][color=darkred]
    >>>> thingCounts[/color][/color]
    > {}
    >
    > Reason tells me it should work for me using...
    >
    > PythonWin 2.3.4 (#53, May 25 2004, 21:17:02) [MSC v.1200 32 bit
    > (Intel)] on win32.
    >
    > ...as it did for you. The runtime interpreter, however, says
    > otherwise. Can you see what I've done wrong...I can't?
    >
    > With thanks in advance, I am,
    >
    > Respectfully yours,
    >
    > Fred Allen[/color]

    Comment

    Working...