Perlish dictionary behavior

Discussion in 'Python' started by Fred Allen, Jun 9, 2004.

  1. Fred Allen

    Fred Allen Guest

    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
     
    Fred Allen, Jun 9, 2004
    #1
    1. Advertising

  2. (Fred Allen) wrote in
    news::

    > 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
     
    Chris Liechti, Jun 9, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. gabriele renzi

    disappearing of global perlish variables

    gabriele renzi, Feb 20, 2004, in forum: Python
    Replies:
    3
    Views:
    273
    gabriele renzi
    Feb 20, 2004
  2. Chris

    Perlish dictionary behavior

    Chris, Jun 3, 2004, in forum: Python
    Replies:
    2
    Views:
    288
    Larry Bates
    Jun 3, 2004
  3. gabriele renzi
    Replies:
    1
    Views:
    117
    Robert Klemme
    Feb 20, 2004
  4. Bob Walton
    Replies:
    9
    Views:
    219
    David K. Wall
    Sep 26, 2003
  5. Peter Michael

    "Perlish Patterns" by Phil Crow

    Peter Michael, Aug 24, 2004, in forum: Perl Misc
    Replies:
    2
    Views:
    149
    Rocco Caputo
    Aug 25, 2004
Loading...

Share This Page