In-place assignment for "boolean or"?

Hi, I find a common idiom in Python is: x = x or 'some other value' This is highly reminiscent of the problem inplace operators solve. Would it be a good idea to consider an inplace operator for this, perhaps: x or= 'some other value' ? Thanks, Cammil

On Mon, Mar 26, 2018 at 01:48:34PM +0100, Cammil Taank wrote:
I don't find it very common. I don't think I've ever used it or seen it. I've occasionally used: spam = eggs or 'something' but mostly I use: result = function(eggs or 'something') (and even that is quite unusual). But others may use it more often.
This is highly reminiscent of the problem inplace operators solve.
Not really: the other augmented operators all connect to ordinary operators that can be overloaded. But `or` is special, it's a short-cutting operator that is treated specially by the interpreter. And even if x is mutable, there's no way to make this an in-place operation. After `x = x or y`, there are only two possibilities: - x is still a reference to whatever it was before the operation; - or x now refers to the same object as y. So this is very different behaviour to (say) += and the other augmented operators.
Would it be a good idea to consider an inplace operator for this, perhaps:
x or= 'some other value'
Mixing a keyword with a symbol like that looks really weird. Others may disagree, but to me, this pattern isn't common enough to justify specialist syntax. I might be a little less negative if the syntax didn't look so strange to me. But only a little. -- Steve

Given the arguments you've raised, I am now inclined to agree with you. However, for completeness of the discussion, I want to indicate the primary intended use case: def fn(x=None): if not x: x = some_complex_or_runtime_dependent_result() For me this appears quite frequently. Admittedly the condition should likely be `x is None`. On 26 March 2018 at 14:41, Steven D'Aprano <steve@pearwood.info> wrote:

See https://www.python.org/dev/peps/pep-0505/ On 26 March 2018 at 14:56, Cammil Taank <ctaank@gmail.com> wrote:

I actually HAVE used this idiom moderately often. But unless you use overly long names, the current spelling is no trouble at all. In fact, it's not clear why your function is spelled as it is rather than with boolean shortcutting (assuming you like the shortcutting aesthetically). On Mon, Mar 26, 2018 at 6:56 AM, Cammil Taank <ctaank@gmail.com> wrote:
However, for completeness of the discussion, I want to indicate the primary intended use case:
def fn(x=None): if not x: x = some_complex_or_runtime_dependent_result() def fn2(x=None): x = x or some_complex_or_runtime_dependent_result() It looks like the boolean shortcutting version makes a line one character longer; but it does save a line and an indent level. Of course, if the 'not x' suite contains multiple statements, you need the 'if' either way. If, as you write, the condition is actually 'x is not None', you want the suite for sure. That's much better than a contorted ternary IMO. There were a couple PEPs about "None-shortcutting" for this not-so-special case of comparing with None. But also IMO, they all looked incredibly ugly for little benefit (lots of non-obvious syntax characters that look like Perl or APL) -- Keeping medicines from the bloodstreams of the sick; food from the bellies of the hungry; books from the hands of the uneducated; technology from the underdeveloped; and putting advocates of freedom in prisons. Intellectual property is to the 21st century what the slave trade was to the 16th.

On 26/03/18 13:48, Cammil Taank wrote:
I believe this is a historic idiom from back before the ternary operator was added to Python: x = x if x else 'some other value' though honestly both of those are poor ways of spelling if not x: x = 'some other value'
This is highly reminiscent of the problem inplace operators solve.
It really isn't, particularly not in Python
Would it be a good idea to consider an inplace operator for this, perhaps:
x or= 'some other value'
I don't find the combination of letters and symbols appealing, sorry. -- Rhodri James *-* Kynesim Ltd

On Mon, Mar 26, 2018 at 01:48:34PM +0100, Cammil Taank wrote:
I don't find it very common. I don't think I've ever used it or seen it. I've occasionally used: spam = eggs or 'something' but mostly I use: result = function(eggs or 'something') (and even that is quite unusual). But others may use it more often.
This is highly reminiscent of the problem inplace operators solve.
Not really: the other augmented operators all connect to ordinary operators that can be overloaded. But `or` is special, it's a short-cutting operator that is treated specially by the interpreter. And even if x is mutable, there's no way to make this an in-place operation. After `x = x or y`, there are only two possibilities: - x is still a reference to whatever it was before the operation; - or x now refers to the same object as y. So this is very different behaviour to (say) += and the other augmented operators.
Would it be a good idea to consider an inplace operator for this, perhaps:
x or= 'some other value'
Mixing a keyword with a symbol like that looks really weird. Others may disagree, but to me, this pattern isn't common enough to justify specialist syntax. I might be a little less negative if the syntax didn't look so strange to me. But only a little. -- Steve

Given the arguments you've raised, I am now inclined to agree with you. However, for completeness of the discussion, I want to indicate the primary intended use case: def fn(x=None): if not x: x = some_complex_or_runtime_dependent_result() For me this appears quite frequently. Admittedly the condition should likely be `x is None`. On 26 March 2018 at 14:41, Steven D'Aprano <steve@pearwood.info> wrote:

See https://www.python.org/dev/peps/pep-0505/ On 26 March 2018 at 14:56, Cammil Taank <ctaank@gmail.com> wrote:

I actually HAVE used this idiom moderately often. But unless you use overly long names, the current spelling is no trouble at all. In fact, it's not clear why your function is spelled as it is rather than with boolean shortcutting (assuming you like the shortcutting aesthetically). On Mon, Mar 26, 2018 at 6:56 AM, Cammil Taank <ctaank@gmail.com> wrote:
However, for completeness of the discussion, I want to indicate the primary intended use case:
def fn(x=None): if not x: x = some_complex_or_runtime_dependent_result() def fn2(x=None): x = x or some_complex_or_runtime_dependent_result() It looks like the boolean shortcutting version makes a line one character longer; but it does save a line and an indent level. Of course, if the 'not x' suite contains multiple statements, you need the 'if' either way. If, as you write, the condition is actually 'x is not None', you want the suite for sure. That's much better than a contorted ternary IMO. There were a couple PEPs about "None-shortcutting" for this not-so-special case of comparing with None. But also IMO, they all looked incredibly ugly for little benefit (lots of non-obvious syntax characters that look like Perl or APL) -- Keeping medicines from the bloodstreams of the sick; food from the bellies of the hungry; books from the hands of the uneducated; technology from the underdeveloped; and putting advocates of freedom in prisons. Intellectual property is to the 21st century what the slave trade was to the 16th.

On 26/03/18 13:48, Cammil Taank wrote:
I believe this is a historic idiom from back before the ternary operator was added to Python: x = x if x else 'some other value' though honestly both of those are poor ways of spelling if not x: x = 'some other value'
This is highly reminiscent of the problem inplace operators solve.
It really isn't, particularly not in Python
Would it be a good idea to consider an inplace operator for this, perhaps:
x or= 'some other value'
I don't find the combination of letters and symbols appealing, sorry. -- Rhodri James *-* Kynesim Ltd
participants (6)
-
Cammil Taank
-
David Mertz
-
Paul Moore
-
Rhodri James
-
Rob Cliffe
-
Steven D'Aprano