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

Discussion in 'Python' started by Erik Max Francis, Jul 21, 2003.

  1. Re: Unfortunate exception on dict item assignment (and why aren't sliceshashable?)

    Ben Finney wrote:

    > You're not requesting to slice a dict; you're requesting to reference
    > an
    > element of a dict with : as the key.


    No he's not, that would be

    {}[':']

    Here's the proof; actually, the error has nothing to do with assignment,
    it just has to do with slicing:

    >>> {}[:]

    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    TypeError: unhashable type

    What this really does is build a slice object and try that:

    >>> {}[slice(0)]

    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    TypeError: unhashable type

    So the complaint is that the index it's trying to lookup can't be
    hashed. And that's because slices aren't hashable:

    >>> hash(slice(0))

    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    TypeError: unhashable type

    Though there's no doubt at all that's a confusing error. But slicing is
    a feature of a _sequence_ type, not a _mapping_ type.

    --
    Erik Max Francis && && http://www.alcyone.com/max/
    __ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
    / \ It is much safer to obey than to rule.
    \__/ Thomas a Kempis
     
    Erik Max Francis, Jul 21, 2003
    #1
    1. Advertising

  2. Erik Max Francis

    Ben Finney Guest

    On Sun, 20 Jul 2003 20:48:16 -0500, Jeff Epler wrote:
    >>>> {}[:] = None

    >
    > I'd have expected something like 'TypeError: unslicable type' meaning
    > that you can't slice a dict, not that slice()s aren't hashable.


    You're not requesting to slice a dict; you're requesting to reference an
    element of a dict with : as the key.

    > In fact, I wonder why slices *aren't* hashable.


    How would you store the resulting key; how would you expect it to be
    returned from keys() ?

    --
    \ "The right to use [strong cryptography] is the right to speak |
    `\ Navajo." -- Eben Moglen |
    _o__) |
    http://bignose.squidly.org/ 9CFE12B0 791A4267 887F520C B7AC2E51 BD41714B
     
    Ben Finney, Jul 21, 2003
    #2
    1. Advertising

  3. Erik Max Francis

    Aahz Guest

    In article <>,
    Jeff Epler <> wrote:
    >
    >I don't see what's problematic about letting slices be hashable: they
    >don't appear to be mutable, for instance.
    >>>> s.start

    >0
    >>>> s.start = -1

    >TypeError: readonly attribute
    >... it may merely be an oversight. Hashing them as
    > hash((s.start,s.stop,s.step))
    >might be a reasonable definition.


    Makes sense to me. Feel free to submit a patch to SF. ;-)
    --
    Aahz () <*> http://www.pythoncraft.com/

    This is Python. We don't care much about theory, except where it intersects
    with useful practice. --Aahz
     
    Aahz, Jul 29, 2003
    #3
  4. "Aahz" <> wrote:
    > Jeff Epler <> wrote:
    > >
    > >I don't see what's problematic about letting slices be hashable:

    > [...]
    >Hashing them as
    > > hash((s.start,s.stop,s.step))
    > >might be a reasonable definition.

    >
    > Makes sense to me. Feel free to submit a patch to SF. ;-)


    I'd rather he didn't. I prefer Python to be able to catch the bug of
    attempting to use a dict as were it a list. For the rare case where
    using a slice key was intentional, converting to tuple form is easy
    enough.

    I can see that it's good for consistency to allow hashing of slices.
    However it's bad for consistency that where alist[slice] returns a
    subsequence, adict[slice] would return a single element.

    - Anders
     
    Anders J. Munch, Jul 29, 2003
    #4
  5. Erik Max Francis

    Aahz Guest

    In article <3f2627c7$0$76067$>,
    Anders J. Munch <> wrote:
    >"Aahz" <> wrote:
    >> Jeff Epler <> wrote:
    >>>
    >>>I don't see what's problematic about letting slices be hashable:

    >> [...]
    >>Hashing them as
    >>> hash((s.start,s.stop,s.step))
    >>>might be a reasonable definition.

    >>
    >> Makes sense to me. Feel free to submit a patch to SF. ;-)

    >
    >I'd rather he didn't. I prefer Python to be able to catch the bug of
    >attempting to use a dict as were it a list. For the rare case where
    >using a slice key was intentional, converting to tuple form is easy
    >enough.
    >
    >I can see that it's good for consistency to allow hashing of slices.
    >However it's bad for consistency that where alist[slice] returns a
    >subsequence, adict[slice] would return a single element.


    Fair enough. Note carefully that I said "submit a patch" -- that
    doesn't guarantee that the patch will be accepted, and it's quite likely
    that it won't be. But "submit a patch" is usually a good way to get
    people to shut up. ;-)
    --
    Aahz () <*> http://www.pythoncraft.com/

    This is Python. We don't care much about theory, except where it intersects
    with useful practice. --Aahz
     
    Aahz, Jul 29, 2003
    #5
  6. Erik Max Francis

    Jeff Epler Guest

    On Tue, Jul 29, 2003 at 07:29:19PM +0100, Marc Wilson wrote:
    > In comp.lang.python, (Aahz) (Aahz) wrote in
    > <bg5vg6$2d2$>::
    >
    > |Fair enough. Note carefully that I said "submit a patch" -- that
    > |doesn't guarantee that the patch will be accepted, and it's quite likely
    > |that it won't be. But "submit a patch" is usually a good way to get
    > |people to shut up. ;-)
    >
    > Shame it doesn't work in *all* newsgroups, eh?


    I'll have you know that I've submitted a number of Python patches that
    were clearly bad ideas. In fact, I'm a little hurt that Aahz doesn't
    think of me as "that guy who often posts or submits horrible patches
    and won't shut up".

    Jeff
     
    Jeff Epler, Jul 30, 2003
    #6
  7. Erik Max Francis

    Aahz Guest

    In article <>,
    Jeff Epler <> wrote:
    >On Tue, Jul 29, 2003 at 07:29:19PM +0100, Marc Wilson wrote:
    >> In comp.lang.python, (Aahz) (Aahz) wrote in
    >> <bg5vg6$2d2$>::
    >>
    >> |Fair enough. Note carefully that I said "submit a patch" -- that
    >> |doesn't guarantee that the patch will be accepted, and it's quite likely
    >> |that it won't be. But "submit a patch" is usually a good way to get
    >> |people to shut up. ;-)
    >>
    >> Shame it doesn't work in *all* newsgroups, eh?

    >
    >I'll have you know that I've submitted a number of Python patches that
    >were clearly bad ideas. In fact, I'm a little hurt that Aahz doesn't
    >think of me as "that guy who often posts or submits horrible patches
    >and won't shut up".


    I'm not sufficiently involved with the actual development process to
    hold such an opinion. ;-) (Despite my regular comments on python-dev,
    I don't contribute code because SF refuses to make changes to allow Lynx
    to work correctly.)
    --
    Aahz () <*> http://www.pythoncraft.com/

    This is Python. We don't care much about theory, except where it intersects
    with useful practice. --Aahz
     
    Aahz, Jul 30, 2003
    #7
  8. [Fredrik Lundh]

    > Aahz wrote:
    > > (Despite my regular comments on python-dev, I don't contribute
    > > code because SF refuses to make changes to allow Lynx to work
    > > correctly.)


    > maybe someone with an SF account could help you out?


    Let me say (and probably repeat) that imposing Web browsers to users,
    or bug trackers with sorrow editing interfaces, and asking users to get
    acquainted with the classification system and various work idiosyncrasies
    of each and every maintainer, package by package, is insane.

    The result is that users have either to choose on which few packages they
    will specialise on, or else, to merely stop contributing. In the former
    case, a maintainer is building his own specialised crowd, which may be
    admittedly good for his project alone. In the latter case, a maintainer
    is pushing users away. But in all cases, this is an overall social lost.

    I miss the times when it was a simple matter to offer contributions,
    maybe humble and small, yet possibly numerous, to any package in sight.
    Maintainers praising want-to-be-sophisticated machinery should use it all
    their soul if they feel like it, but without forcing users into their bag..

    --
    Fran├žois Pinard http://www.iro.umontreal.ca/~pinard
     
    Francois Pinard, Jul 31, 2003
    #8
  9. I think I've ranted, along these lines, in this forum, before.
    However, SF has just gone down for maintenence as I try to comment on
    a bug, so I'm feeling these issues (and am being frustrated in my
    attempts to "do something useful instead").

    Francois Pinard <> writes:

    > [Fredrik Lundh]
    >
    > > Aahz wrote:
    > > > (Despite my regular comments on python-dev, I don't contribute
    > > > code because SF refuses to make changes to allow Lynx to work
    > > > correctly.)

    >
    > > maybe someone with an SF account could help you out?

    >
    > Let me say (and probably repeat) that imposing Web browsers to users,
    > or bug trackers with sorrow editing interfaces, and asking users to get
    > acquainted with the classification system and various work idiosyncrasies
    > of each and every maintainer, package by package, is insane.


    W W EEEEE K K N N OOO W W
    W W E K K NN N O O W W
    W W W EEE KK N N N O O W W W
    WW WW E K K N NN O O WW WW
    W W EEEEE K K N N OOO W W

    that the SF trackers are not ideal. We are trying, hoping, to move to
    a friendly system (roundup) more under our control, but there's this
    small matter of time...

    > The result is that users have either to choose on which few packages they
    > will specialise on, or else, to merely stop contributing. In the former
    > case, a maintainer is building his own specialised crowd, which may be
    > admittedly good for his project alone. In the latter case, a maintainer
    > is pushing users away. But in all cases, this is an overall social lost.


    If we push away contributors who do not have time to adjust to using a
    different tool for the Python project, maybe that's not such a bad
    thing.

    Submitting a patch to Python implies a certain amount of commitment.
    You just cannot expect the (volunteer!) maintainers to fall down and
    be grateful that someone has deigned to send a patch their way.

    > I miss the times when it was a simple matter to offer contributions,
    > maybe humble and small, yet possibly numerous, to any package in sight.
    > Maintainers praising want-to-be-sophisticated machinery should use it all
    > their soul if they feel like it, but without forcing users into their bag.


    Look, we get more patches than we can cope with. We *have* to have
    some way of organising our backlog. What do you suggest instead?
    That you just email a patch to one of the maintainers and have them
    shuffle it into the structured tracker, thus taking time away from
    them that they could be using to assess the patch? Get real. Or,
    better, offer to help set up roundup on python.org.

    And frankly, the pain of using suboptimal trackers is felt *much more*
    by the maintainers than those who submit patches to us.

    Cheers,
    mwh

    --
    [2. More type system hacking --- text/plain; type-argh.diff]
    -- csr on sbcl-devel
     
    Michael Hudson, Aug 1, 2003
    #9
  10. "Fredrik Lundh" <> writes:

    > Aahz wrote:
    > > (Despite my regular comments on python-dev, I don't contribute
    > > code because SF refuses to make changes to allow Lynx to work
    > > correctly.)

    >
    > maybe someone with an SF account could help you out? could it be
    > worth setting up an aahz-helpers mailing list somewhere, manned by
    > volunteers with SF access and modern browsers?


    I've successfully used w3m to drive SF's website, FWIW.

    Cheers,
    mwh

    --
    42. You can measure a programmer's perspective by noting his
    attitude on the continuing vitality of FORTRAN.
    -- Alan Perlis, http://www.cs.yale.edu/homes/perlis-alan/quotes.html
     
    Michael Hudson, Aug 1, 2003
    #10
  11. Erik Max Francis

    Aahz Guest

    In article <>,
    Michael Hudson <> wrote:
    >"Fredrik Lundh" <> writes:
    >> Aahz wrote:
    >>>
    >>> (Despite my regular comments on python-dev, I don't contribute
    >>> code because SF refuses to make changes to allow Lynx to work
    >>> correctly.)

    >>
    >> maybe someone with an SF account could help you out? could it be
    >> worth setting up an aahz-helpers mailing list somewhere, manned by
    >> volunteers with SF access and modern browsers?


    <chuckle> I suppose I'll break down one of these years and find better
    software.

    >I've successfully used w3m to drive SF's website, FWIW.


    I've used both w3m and links, and I find them much more difficult to use
    than lynx.
    --
    Aahz () <*> http://www.pythoncraft.com/

    This is Python. We don't care much about theory, except where it intersects
    with useful practice. --Aahz
     
    Aahz, Aug 2, 2003
    #11
    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. Bengt Richter
    Replies:
    2
    Views:
    297
    Jp Calderone
    Jul 21, 2003
  2. CPIM Ronin

    Unfortunate newbie questions!

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

Share This Page