On Mon, Oct 14, 2019 at 11:26 AM Steve Jorgensen <ste...@stevej.name> wrote:
>
> Steven D'Aprano wrote:
> > On Sun, Oct 13, 2019 at 05:42:21PM -0400, David Mertz wrote:
> > > I have definitely hit this difficulty many times. The
> > > part of the code that
> > > "just puts something in the collection" doesn't need to care conceptually
> > > about the kind of collection. Taking the data back out somewhere else more
> > > often needs to worry about order, efficiency, concurrency, etc.
> > > And that's why I am skeptical that there is "very common" code that
> > doesn't care about whether it has a list or a set. We surely almost
> > always will care when we're taking the data out again, because of
> > ordering and efficiency etc.
> > If you require a set for the data-output side, why would you accept a
> > list for the data-input side when you won't be able to use it further
> > on? And vice versa.
> > In the absense of any concrete examples demonstrating that this is
> > useful, I think this is an over-generalisation of little practical use.
>
> Having come from Ruby to Python, this seems like an odd statement to me. It 
> always felt very natural that there is one way to add something to a simple 
> collection, and the distinction between an "array" and a set is irrelevant in 
> that regard. Of course, just because I'm used to that as a natural thing 
> doesn't make it right for Python.
>

Sets are somewhat like lists, but also somewhat like dictionaries. A
set is a list that doesn't allow duplicates, or it's a dictionary
where the values are irrelevant. If you think of a set as more
list-like, then it's logical to "set.append(x)"; if you think of it as
more dict-like, you should "set[x]=True". It's not quite either, which
is why a generic "stick this thing in that collection" doesn't really
exist.

Though a set.__setitem__() method might be helpful here. If you set an
item to any truthy value, it is added to the set; set it to a falsy
value and it will be discarded (without error if it wasn't there, so
assignment in this way is idempotent). That would make them more
similar to collections.Counter, which can be seen as a multiset of
sorts.

ChrisA
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/W7XZA4FLJKWZHMIDLQ4MZRK34IMNTM6Y/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to