On Mon, Sep 21, 2015 at 1:50 PM, Steven D'Aprano email@example.com wrote:
On Sun, Sep 20, 2015 at 07:38:18PM +1000, Chris Angelico wrote:
On Sun, Sep 20, 2015 at 5:31 PM, Steven D'Aprano firstname.lastname@example.org wrote:
Technically, x.y x[y] and x(y) aren't operators, but for the sake of convenience I'll call them such. Even though these are binary operators, the ? only shortcuts according to the x, not the y. So we can call these ?. ? ?() operators "pseudo-unary" operators rather than binary operators.
That's how all Python's short-circuiting works - based on the value of what's on the left, decide whether or not to evaluate what's on the right. (Well, nearly all - if/else evaluates the middle first, but same difference.) This is another form of short-circuiting; "x[y]" evaluates x, then if that's None, doesn't bother evaluating y because it can't affect the result.
I think you are mistaken about x[y]:
py> None[print("side effect")] side effect Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'NoneType' object is not subscriptable
That's why x?[y] is a proposal.
Oops, that was a typo in my statement. I meant "x?[y]" should behave that way - once it's discovered that x is None, the evaluation of y can't affect the result, and so it doesn't get evaluated (as per the normal short-circuiting rules). Yes, x[y] has to evaluate both x and y (after all, the value of y is passed to __getitem__). Sorry for the confusion.