Re: Unfortunate exception on dict item assignment (and why aren't slices hashable?)

Discussion in 'Python' started by Bengt Richter, Jul 21, 2003.

  1. On Sun, 20 Jul 2003 20:48:16 -0500, Jeff Epler <> wrote:

    >>>> {}[:] = None

    >Traceback (most recent call last):
    > File "<stdin>", line 1, in ?
    >TypeError: unhashable type
    >
    >I'd have expected something like 'TypeError: unslicable type'
    >meaning that you can't slice a dict, not that slice()s aren't hashable.
    >
    >In fact, I wonder why slices *aren't* hashable. I can imagine wanting
    >to write something like the following:

    Seems like you could (untested!)
    > class X:

    from types import SliceType # BTW, why is slice a builtin function, not a type?
    > # ...
    > def __getitem__(self, idx):

    if isinstance(idx, self.SliceType): idx = (idx.start, idx.stop, idx.step)
    # and adjust self.calculate_xxx to match.
    > if not idx in self._cache:
    > # potentially expire an item and
    > if isinstance(idx, slice):
    > self._cache[idx] = self.calculate_slice_expensively(idx)
    > else:
    > self._cache[idx] = self.calculate_idx_expensively(idx)
    > return self._cache[idx]
    >
    >Jeff
    >


    Regards,
    Bengt Richter
    Bengt Richter, Jul 21, 2003
    #1
    1. Advertising

  2. Bengt Richter

    Terry Reedy Guest

    "Bengt Richter" <> wrote in message
    news:bffq56$j27$0@216.39.172.122...
    > from types import SliceType # BTW, why is slice a builtin

    function, not a type?

    I presume you are asking: Why hasn't slice been converted from a
    'mere' function to a callable type like int, str, and float have been?

    If this has not yet been done for the coming 2.3, I presume it will be
    as soon as someone capable of doing the conversion decides to do so.
    Slice was originally added for use by Numerical Extensions and so far
    has probably seen little use by anyone else. Hence its conversion is
    of lesser priority.

    Terry J. Reedy
    Terry Reedy, Jul 21, 2003
    #2
    1. Advertising

  3. Bengt Richter

    Jp Calderone Guest

    On Mon, Jul 21, 2003 at 01:22:07AM -0400, Terry Reedy wrote:
    >
    > I presume you are asking: Why hasn't slice been converted from a
    > 'mere' function to a callable type like int, str, and float have been?
    >
    > If this has not yet been done for the coming 2.3, \
    > [snip]


    It has.

    Jp

    --
    Lowery's Law:
    If it jams -- force it. If it breaks, it needed replacing anyway.

    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.2.0 (GNU/Linux)

    iD8DBQE/G4YLedcO2BJA+4YRAghVAJ9JQw3Xp2m8bxuSwo7i34OizJ4ZhQCgu569
    YyHBSPl/vaVJsZVzojk6cuo=
    =yzTJ
    -----END PGP SIGNATURE-----
    Jp Calderone, Jul 21, 2003
    #3
    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. Erik Max Francis
    Replies:
    10
    Views:
    524
  2. CPIM Ronin

    Unfortunate newbie questions!

    CPIM Ronin, Sep 12, 2005, in forum: Python
    Replies:
    5
    Views:
    306
    Chris Lasher
    Sep 13, 2005
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,656
    Smokey Grindel
    Dec 2, 2006
  4. Neal Becker
    Replies:
    14
    Views:
    1,176
    Aaron \Castironpi\ Brady
    Sep 25, 2008
  5. Wells
    Replies:
    6
    Views:
    4,306
    Syeberman
    Nov 10, 2009
Loading...

Share This Page