[Python-ideas] Adding __getter__ to compliment __iter__.

Steven D'Aprano steve at pearwood.info
Sun Jul 21 07:33:59 CEST 2013


On 21/07/13 14:04, Ron Adam wrote:

> You seem to be stuck on this point.  The exact syntax isn't important.  You are clearly -1 on this particular spelling.  That's fine.

Ron, either I haven't explained myself clearly enough, or you haven't been paying attention :-) I'm not opposed to this particular spelling. I'm opposed to optimizing method calls with syntax *regardless of the spelling*.

This opposition should be considered provisional. Obviously if there is a good enough reason to give something syntax, like item lookups have syntax seq[item], then I do not oppose it. But as a general rule, Python does not add syntax for every little thing that might happen to be micro-optimized by a special byte-code.

Let me put it this way, to give an analogy... in some of my code, I have a class with a "calculate" method. I use this class *all the time*, it is really important to me. If I were to propose special syntax to call the "calculate" method, so as to avoid the method lookup and argument passing overhead, I would expect that most people would ask the same question I asked earlier:

What's so special about this that it needs dedicated syntax?

"It will be a tiny bit faster, because it avoids the method call lookup" is not an answer. That would be true for *any* method. What is so special about *this method* (whether it is "calculate" or "send" that it is worth paying the extra cost of new syntax in order to avoid that overhead? Normally Python only uses syntax for things that people absolutely expect to be syntax (like operators), or to give features that can't (conveniently, or at all) work as regular function calls (like del and import).

If you have a good answer to that question, then I might change my mind and support your proposal.


[...]
> The thing that got me on this is, if generators aren't faster than a class with method calls.  Then why do we generators?

Because typically a generator is much easier to read and write. And also because once you have the infrastructure to support generators, that can be generalised to give you coroutines as well.

Besides, are you sure that generators aren't faster?



-- 
Steven


More information about the Python-ideas mailing list