Perlish dictionary behavior
Chetan Gadgil
cgadgil_list at cxoindia.dnsalias.com
Tue Jun 8 22:44:19 EDT 2004
>>> class AllThingsStartAtZero(dict):
... def __getitem__(self, key):
... return dict.get(self, key, 0)
...
> -----Original Message-----
> From:
> python-list-bounces+cgadgil_list=cxoindia.dnsalias.com at python.
> org
> [mailto:python-list-bounces+cgadgil_list=cxoindia.dnsalias.com
> @python.org] On Behalf Of Fred Allen
> Sent: Wednesday, June 09, 2004 6:24 AM
> To: python-list at python.org
> Subject: Re: Perlish dictionary behavior
>
>
> Mr. Brewer:
>
> I fruitlessly tried your "thing counter", as you can see below.
>
> >>> class AllThingsStartAtZero(dict):
> ... def getitem (self, key):
> ... 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
> --
> http://mail.python.org/mailman/listinfo/python-list
>
More information about the Python-list
mailing list