del and sets proposal

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Steven D'Aprano

    #16
    Re: del and sets proposal

    On Sun, 05 Oct 2008 22:11:38 +1300, Lawrence D'Oliveiro wrote:
    In message <00f80c6a$0$206 33$c3e8da3@news .astraweb.com>, Steven D'Aprano
    wrote:
    >
    >Would it really be "confusing" if sets used the same interface as dicts
    >use? I don't think so. What else could "del aset[x]" mean other than
    >"delete element x"?
    >
    Yes, but "x" in what sense?
    The only sense possible.

    If I wrote aset.remove(x) would you ask "what's x, a key or a value?" No
    of course you wouldn't, because the question is nonsense for sets. The
    same goes for "x in aset", "aset.add(x )" and any other set method that
    takes an element as an argument. Why are you singling out del for this
    pretend confusion?

    In dicts it's a key, in sets, shouldn't it
    also be a key and not a value?
    That question isn't even meaningful. Sets have elements. The semantics of
    set elements is closer to dictionary keys than to dictionary values:

    x in adict # is key x in dictionary?
    x in aset # is element x in set?

    and an early implementation of Python sets used dictionaries, where the
    elements where stored as keys, not values. It makes no sense to treat set
    elements as if they were analogous to dict values.

    Sets have no keys, only values. One might suggest assignment of keys, à
    la array indexes, but that means assigning an ordering to the values,
    whereas sets are explicitly unordered.
    Why on earth should "del aset[x]" imply that sets are ordered? Dicts
    aren't ordered. You don't find people going "wibble wibble wibble, I
    don't understand del adict[x] because dicts aren't ordered, what could it
    mean???"


    --
    Steven

    Comment

    • Aaron \Castironpi\ Brady

      #17
      Re: del and sets proposal

      Bruno Desthuilliers wrote:
      Steven D'Aprano a écrit :
      >On Sat, 04 Oct 2008 18:36:28 +0200, Bruno Desthuilliers wrote:
      >>
      >Lists are the odd one out, because del alist[x] is used to remove the
      >element at position x, rather than removing an element x.
      >
      Nope. It's perfectly consistent with dicts, where del adict[x] is used
      to remove element associated to key x, not to remove element with value
      x. Lists and dicts are both indexed collections, list being indexed by
      position and dicts by key. sets are not indexed.
      >
      (snip remaining - I could only keep on repeating the same thing).
      --
      http://mail.python.org/mailman/listinfo/python-list
      Which one has seniority? It's somewhat arbitrary which of 'pop' and
      'remove' have which meanings in lists. You can 'remove' on index or
      value in lists, key or value in dicts, and only value in sets.

      I agree that you don't pass an 'index' to 'del' in dicts, you pass a
      key. Does the OP also hold that dicts should grow a 'remove' method to
      remove a value?

      Comment

      • Lawrence D'Oliveiro

        #18
        Re: del and sets proposal

        In message <00f88c9e$0$206 33$c3e8da3@news .astraweb.com>, Steven D'Aprano
        wrote:
        On Sun, 05 Oct 2008 22:11:38 +1300, Lawrence D'Oliveiro wrote:
        >
        >In message <00f80c6a$0$206 33$c3e8da3@news .astraweb.com>, Steven D'Aprano
        >wrote:
        >>
        >>Would it really be "confusing" if sets used the same interface as dicts
        >>use? I don't think so. What else could "del aset[x]" mean other than
        >>"delete element x"?
        >>
        >Yes, but "x" in what sense?
        >
        The only sense possible.
        There is no precedent for using a set element as a subscript to the set.

        Comment

        Working...