[Python-ideas] unify usage of mutable and immutable objects
David Mertz
mertz at gnosis.cx
Tue Feb 28 20:32:06 EST 2017
On Tue, Feb 28, 2017 at 1:49 PM, Terry Reedy <tjreedy at udel.edu> wrote:
> On 2/28/2017 1:48 PM, David Mertz wrote:
>
>> In [30]: class FrozenSet(frozenset):
>> ...: def pop(self):
>> ...: item = next(iter(self))
>> ...: return item, self-{item}
>>
>> In [31]: a = FrozenSet({1,2,3,4,5})
>>
>> In [32]: a.pop()
>> Out[32]: (1, frozenset({2, 3, 4, 5}))
>>
>
> To me, 'pop' implies mutation. Tuples do not have a pop method, and it is
> not obvious to me that either tuples or frozensets should. What are the
> use cases that are not better served by converting to list or set?
Yeah, I guess you are right. I was thinking that having a compatible API
for sets and frozensets would be good. But this really isn't it anyway.
`.pop()` is one of those few standard library methods that is used for
simultaneous mutation and return value. My FrozenSet class does something
quite different with the method; possibly something useful enough to have
under a different name, but it doesn't help with duck typing set-like
objects.
The OP suggests:
Pop tuple/frozenset(standard one) gain no benefit. # O(n)
> It is a different story for balanced tree. # O(log n)
This is true. You can make a slightly changed copy of an immutable tree in
O(log N) time since most pointers are to existing subtrees. But then,
pretty much everything else one does is also O(log N) which loses to the
O(1) of most set operations.
But that's moot for Python. A custom class implementing a balanced tree is
free to define its own semantics for a `.pop()` method. This is a very
different data structure than Python's set/frozenset which are based on
hashes.
We are not going to change the implementation of sets so fundamentally,
mostly because having O(1) behaviors is so nice (add, remove, membership
check, etc).
--
Keeping medicines from the bloodstreams of the sick; food
from the bellies of the hungry; books from the hands of the
uneducated; technology from the underdeveloped; and putting
advocates of freedom in prisons. Intellectual property is
to the 21st century what the slave trade was to the 16th.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20170228/f8a4ca4d/attachment.html>
More information about the Python-ideas
mailing list