proposed proposal: set.values()

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Paul Rubin

    #1

    proposed proposal: set.values()

    I'm thinking of proposing that a values method be added to set
    objects, analogously with dicts. If x is a set, x.values() would be
    the same as list(x). This feels logical, and it would allow unified
    treatment of dicts and sets in some contexts. Any objections?
  • Terry Reedy

    #2
    Re: proposed proposal: set.values()


    "Paul Rubin" <"http://phr.cx"@NOSPAM. invalid> wrote in message
    news:7x1wwjfr9k .fsf_-_@ruckus.brouha ha.com...[color=blue]
    > I'm thinking of proposing that a values method be added to set
    > objects, analogously with dicts. If x is a set, x.values() would be
    > the same as list(x). This feels logical, and it would allow unified
    > treatment of dicts and sets in some contexts. Any objections?[/color]

    1. It is pure duplication that *adds* keystrokes.

    2. It copies the wrong aspect of dict. A set is like dict.keys (no
    duplicates, hashable), not dict.values (duplicates and non-hashables ok).

    3. It copies a workaround. Conceptually, dict.keys() and dict.items()
    should each be a set, not list, and would have been if Python had had sets
    at birth. Dict.values() should be a multiset or bag. The order of any is
    purely artificial and random. Set.keys() or set.values() should be the set
    itself.

    4. The workaround will change or even better go away. In 3.0, ,keys,
    ..values and .items not be lists. The initial proposal was to replace them
    with iterators (the current iterkeys, etc). A current proposal (still in
    development) is for an iterable set- or multiset-like view on the
    underlying dict.

    Terry Jan Reedy



    Comment

    • Paul Rubin

      #3
      Re: proposed proposal: set.values()

      "Terry Reedy" <tjreedy@udel.e du> writes:[color=blue]
      > 1. It is pure duplication that *adds* keystrokes.[/color]

      Nobody says you shouldn't use list(s) if you know you're dealing with
      a set. The idea of s.values() is so you can duck-type between dicts
      and sets.
      [color=blue]
      > 2. It copies the wrong aspect of dict. A set is like dict.keys (no
      > duplicates, hashable), not dict.values (duplicates and non-hashables ok).[/color]

      I'd say keys is incorrect, since sets don't have keys:
      [color=blue][color=green][color=darkred]
      >>> import sets
      >>> s=sets.Set((1,2 ,3))
      >>> s[1][/color][/color][/color]
      Traceback (most recent call last):
      File "<stdin>", line 1, in ?
      TypeError: unindexable object

      I don't think it's important that some values that can occur in dicts
      (e.g. non-hashables) can't occur in sets. There are similarly values
      for complex numbers that can't be expressed as floats, but that
      doesn't mean __add__ shouldn't work on both.
      [color=blue]
      > 3. It copies a workaround. Conceptually, dict.keys() and dict.items()
      > should each be a set, not list, and would have been if Python had had sets
      > at birth. Dict.values() should be a multiset or bag. The order of any is
      > purely artificial and random. Set.keys() or set.values() should be the set
      > itself.[/color]

      I guess it's ok if sets.items() is the same as sets.values(). Sets
      don't have keys. dict.values() is what it is for historical reasons
      as you state, and would be hard to change, so it makes sense for
      set.values() to work the same way.
      [color=blue]
      > 4. The workaround will change or even better go away. In 3.0, ,keys,
      > .values and .items not be lists. The initial proposal was to replace them
      > with iterators (the current iterkeys, etc). A current proposal (still in
      > development) is for an iterable set- or multiset-like view on the
      > underlying dict.[/color]

      I hadn't heard of this but it does make some sense. However,
      sets.values (and maybe sets.items) should be treated the same way,
      under my proposed proposal.

      Comment

      • Fredrik Lundh

        #4
        Re: proposed proposal: set.values()

        Paul Rubin wrote:
        [color=blue][color=green]
        > > 1. It is pure duplication that *adds* keystrokes.[/color]
        >
        > Nobody says you shouldn't use list(s) if you know you're dealing with
        > a set. The idea of s.values() is so you can duck-type between dicts
        > and sets.[/color]

        if y is a dict, "x in y" looks for a matching key, not for a matching value.

        </F>



        Comment

        • Paul Rubin

          #5
          Re: proposed proposal: set.values()

          "Fredrik Lundh" <fredrik@python ware.com> writes:[color=blue][color=green]
          > > Nobody says you shouldn't use list(s) if you know you're dealing with
          > > a set. The idea of s.values() is so you can duck-type between dicts
          > > and sets.[/color]
          >
          > if y is a dict, "x in y" looks for a matching key, not for a
          > matching value.[/color]

          Good point, the duck typing mismatches on "x in y" and there's nothing
          that can be done about that.

          Imagine a Bag (multiset) object; it can have multiple occurrences of a
          single value. A keys() operation on it should return unique items,
          but values() should return the multiple occurrences.

          Comment

          • Paul Rubin

            #6
            Re: proposed proposal: set.values()

            "Terry Reedy" <tjreedy@udel.e du> writes:[color=blue]
            > 1. It is pure duplication that *adds* keystrokes.
            >[/color]
            Nobody says you shouldn't use list(s) if you know you're dealing with
            a set. The idea of s.values() is so you can duck-type between dicts
            and sets.
            [color=blue]
            > 2. It copies the wrong aspect of dict. A set is like dict.keys (no
            > duplicates, hashable), not dict.values (duplicates and non-hashables[/color]
            ok).[color=blue]
            >[/color]
            I'd say keys is incorrect, since sets don't have keys:
            [color=blue]
            > import sets
            > s=sets.Set((1,2 ,3))
            > s[1]
            > Traceback (most recent call last):[/color]
            File "<stdin>", line 1, in ?
            TypeError: unindexable object

            I don't think it's important that some values that can occur in dicts
            (e.g. non-hashables) can't occur in sets. There are similarly values
            for complex numbers that can't be expressed as floats, but that
            doesn't mean __add__ shouldn't work on both.
            [color=blue]
            > 3. It copies a workaround. Conceptually, dict.keys() and[/color]
            dict.items()[color=blue]
            > should each be a set, not list, and would have been if Python had[/color]
            had sets[color=blue]
            > at birth. Dict.values() should be a multiset or bag. The order of[/color]
            any is[color=blue]
            > purely artificial and random. Set.keys() or set.values() should be[/color]
            the set[color=blue]
            > itself.
            >[/color]
            I guess it's ok if sets.items() is the same as sets.values(). Sets
            don't have keys. dict.values() is what it is for historical reasons
            as you state, and would be hard to change, so it makes sense for
            set.values() to work the same way.
            [color=blue]
            > 4. The workaround will change or even better go away. In 3.0,[/color]
            ,keys,[color=blue]
            > .values and .items not be lists. The initial proposal was to[/color]
            replace them[color=blue]
            > with iterators (the current iterkeys, etc). A current proposal[/color]
            (still in[color=blue]
            > development) is for an iterable set- or multiset-like view on the
            > underlying dict.
            >[/color]
            I hadn't heard of this but it does make some sense. However,
            sets.values (and maybe sets.items) should be treated the same way,
            under my proposed proposal.

            Comment

            • Ron Adam

              #7
              Re: proposed proposal: set.values()

              Paul Rubin wrote:[color=blue]
              > "Terry Reedy" <tjreedy@udel.e du> writes:[color=green]
              >> 1. It is pure duplication that *adds* keystrokes.
              >>[/color]
              > Nobody says you shouldn't use list(s) if you know you're dealing with
              > a set. The idea of s.values() is so you can duck-type between dicts
              > and sets.[/color]

              You could just do the following...

              [color=blue][color=green][color=darkred]
              >>> class vset(set):[/color][/color][/color]
              .... values = set.copy
              ....
              [color=blue][color=green][color=darkred]
              >>> s = vset([1,2,3])[/color][/color][/color]
              [color=blue][color=green][color=darkred]
              >>> s.values()[/color][/color][/color]
              vset([1, 2, 3])
              [color=blue][color=green][color=darkred]
              >>> for x in s.values():[/color][/color][/color]
              .... x
              ....
              1
              2
              3


              Cheers,
              Ron

              Comment

              Working...