On Oct 13, 2019, at 17:25, Steve Jorgensen <ste...@stevej.name> wrote:
> 
> Thinking about examples, the ones that immediately come to mind are mainly 
> collectors — objects passed into a method to record things that will be 
> examined afterward by the caller. In those cases, what I'm talking about 
> could actually be handled (actually with more versatility) by having the 
> called method expect a callable though, since it will only do one thing with 
> the object that is passed in. The caller would then pass in `<some-set>.add`, 
> `<some.list>.append` in that case (or whatever else is appropriate).
> 
> That, of course, is simply a coding pattern and doesn't require any changes 
> to the standard lib.

I like the way C++ handles this, actually. The C++ notion of “Iterator” is a 
lot more complicated than the Python notion of “Iterator”, and usually, this is 
a huge pain, but occasionally, it allows for some nice things that you can’t do 
in Python.

One of those things is that collectors don’t take a collection to add to, they 
take an “output Iterator”.  The caller decides what to pass in. If you’ve got a 
vector (like a Python list), that’s usually a back inserter (which just appends 
to the end). If you’ve got an ordered set or a hash set, it’s usually a set 
inserter. If you’ve got a deque, it’s often a back inserter, but it might be a 
front inserter instead. If you’ve got a threaded queue, it’s a synchronized 
back inserter, or maybe a cooperatively-lock-free one. If you’ve got a 
double-linked list, it could be a back inserter, a front inserter, or even a 
splice inserter at a particular location. And so on. The burden to choose is on 
the caller of the collector, who knows what the right thing to do is for its 
use of its collection, rather than on the collector implementation, which is 
generic and stupid.

Anyway, this doesn’t come up that often in Python, but when it does, passing a 
callable, as you suggest, is usually the best solution.

So, I think you’ve just found some of those cases, and found the best solution 
to them relatively easily, without needing anything to be changed. Which 
implies that Python is fine as it is. :)
_______________________________________________
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/W3SEAQ7NUXPSB5X5DHZLLYODNMOUKGJC/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to