On Oct 13, 2019, at 12:20, Steve Jorgensen <ste...@stevej.name> wrote:
> 
> Failing fast is good, but it is also a very common case to want to add an 
> item to a collection regardless of whether it is set-like or sequence-like.
> 
> Does the fact that the `append` and `add` methods would still exist mitigate 
> your objection? Call `append` when expecting a list, call `add` when 
> expecting a set, or call `push` or use `<<` when either of those would be 
> appropriate targets.

Is there any reason this has to be a method? You can write a function that does 
this today:

    def push(collection, value):
        if isinstance(collection, collections.abc.MutableSet):
            collection.add(value)
        elif isinstance(collection, collections.abc.MutableSequence):
            collection.append(value)
        else:
            raise TypeError(f"Don’t know how to push to 
'{type(collection).__name__}' objects")

This works with existing code today. And it allows you to customize it to 
handle not-quite-sequence types like a circular buffer that you found off PyPI 
without modifying those types. If you expect to do a lot of that, 
`singledispatch` might be better than explicit type-switching code. Or, if you 
have a lot of your own classes that you can modify that aren’t quite sequences 
but want to be pushable, you can even add your own protocol method that it 
checks for first.

_______________________________________________
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/OKGTELX5YME5VJUIQ2OYOJ5XY7SEKTM4/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to