Perlish dictionary behavior
Chris Liechti
cliechti at gmx.net
Tue Jun 8 20:18:35 EDT 2004
fallen at leveltwo.com (Fred Allen) wrote in
news:72976037.0406081556.6d4dd9e7 at posting.google.com:
> Mr. Brewer:
>
> I fruitlessly tried your "thing counter", as you can see below.
>
>>>> class AllThingsStartAtZero(dict):
> ... def getitem (self, key):
it's __getitem__ note the underlines.
> ... return dict.get(self, key, 0)
> ...
>>>> thingCounts = AllThingsStartAtZero()
>>>> for thing in ('a','b','c','d','a'):
> ... thingCounts[thing] += 1
> ...
> Traceback (most recent call last):
> File "<interactive input>", line 2, in ?
> KeyError: 'a'
>>>> thingCounts
> {}
>
> 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
More information about the Python-list
mailing list