
On Thu, Dec 31, 2020 at 3:13 PM Brendan Barnwell brenbarn@brenbarn.net wrote:
On 2020-12-29 15:01, Christopher Barker wrote:
along with a COMPLETE list of the language features that make use of that protocol.
That is pretty much impossible -- that's kind of the point of a protocol -- it can be used in arbitrary places in arbitrary code. would you expect a COMPLETE list of all the language features that use the iteration protocol?
Yes, but perhaps not as complete as you thought I meant. :-) What I
mean by "language features" here is basically syntactic features that IMPLICITLY invoke the protocol.
Certainly arbitrary code can use a protocol the sense that any function
can call some other function that eventually winds up using the protocol. Also, code can explicitly invoke the iterator protocol, by, e.g., calling obj.__next__(), but that's not a problem because you can easily look up how the object defines __next__. The magic part is when you have something like `for x in obj`, which includes not even any indirect references (i.e., in called functions) to `__iter__` or `__next__`, yet winds up calling them. And it's because that is magic that we need to make it very explicit.
That seems pretty reasonable actually. That sort of list wouldn't mention __setstate__, for instance, but it would include everything that can be invoked in some way that doesn't look like a function call. So that would include object lifecycle methods (__new__, __init__, __del__), all your operator functions (__{,r,i}add__ etc), and everything that is necessary to make something behave as if it were some type of thing (__call__ to be callable, __iter__ to be iterable, __enter__/__exit__ to be, uhh, "withable"?).
This would be quite a large list, though, and it would have to cope with odd edge cases like this:
class Foo: def __getitem__(self, x): if x in range(10): return x * x raise IndexError
list(Foo())
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
It's fully iterable even though it has no __iter__ method.
But this would be a useful piece of reference documentation - all dunders (and any non-dunders) called by the language itself. It'd be very detaily and not useful to most people, but could be of interest.
Is there any way to search the CPython source code for all use of dunders, without getting hits for all the places where they're defined?
ChrisA