On Jun 26, 2019, at 21:45, Stephen J. Turnbull <turnbull.stephen.fw@u.tsukuba.ac.jp> wrote:
Chris Angelico writes:
Then I completely don't understand getself. Can you give an example of how it would be used? So far, it just seems like an utter total mess.
It's possible that __getself__ would be implemented "halfway". That is, if __getself__ is present, it is invoked, and performs *side effects* (a simple example would be an access counter/logger for the object). Then the compiler loads that object, discarding any return value of the method. I think this is the semantics that the proponents are thinking of.
The compiler has to load the object before calling __getself__, not after, or it has nothing to call __getself__ on, right? Anyway, I don’t think this really avoids most of the problems. It does get rid of the danger of infinite regress, but I think Nate already solved that. And I think everything else is an inherent consequence of trying to answer “I want to hook this operation on variables” with “here’s an hook on values instead”. Values aren’t the same thing as variables. And there doesn’t seem to be any way to avoid that with an OO dunder protocol, because variables aren’t objects, only values are. But why does it have to be OO? Tcl had this functionality decades ago, “trace variable spam read counter” and now every read of the variable “spam” (not every read of the current value in “spam” no matter where from) calls counter(). It was used for all kinds of things besides the debugging purpose it was originally added for. In fact, Python developers still use it with Tk, to hook UI bits where Tk forgot to add events or validators. And actually, you can already do that in Python: set f_trace_opcodes on the frame and sys.settrace, then on every opcode, if it’s the right kind of LOAD and the arg is the right name, trigger the callback. Of course this is slow and clumsy, but it means someone can build an example today and show “Look, this works, and it’s useful, except that it’s way too slow. Unless you’ve got a better way to make this efficient enough, that’s exactly why I need this feature.”