[Python-ideas] Proposal for extending the collections module - bags / multisets, ordered sets / unique lists

Leonardo Santagada santagada at gmail.com
Sun Jul 19 09:00:48 CEST 2009


On Jul 19, 2009, at 1:31 AM, Mike Meyer wrote:

> On Sun, 19 Jul 2009 11:28:37 +1000
> Steven D'Aprano <steve at pearwood.info> wrote:
>>>> for card in cards:
>>>>    if hand.count(card) == 2:
>>>>        print('You have a pair.')
>> Why would a class with unique=True have a count() method?
>
> So that you can write code that doesn't care which of the various
> flavors of collection it gets passed, but will work on them
> anyway.
>
> It supports a property of classes in OO programming called
> "polymorphism". It's a good thing. That python's collections don't
> have it has always been a minor wart, and it gets bigger as we get
> more types of collections.


For example ints don't support slicing and that is not an omission of  
polymorphism, there is no sense in having that on them. A dict or set  
having a count method would make no sense either. This has nothing to  
do with polymorphism.

add on lists or append on sets could be discussed (maybe append on  
sets makes more sense as they already have pop), but adding count in  
dict or set would not be pragmatic.

--
Leonardo Santagada
santagada at gmail.com






More information about the Python-ideas mailing list