[Python-ideas] In-place assignment for "boolean or"?

Paul Moore p.f.moore at gmail.com
Mon Mar 26 10:34:00 EDT 2018


See https://www.python.org/dev/peps/pep-0505/

On 26 March 2018 at 14:56, Cammil Taank <ctaank at gmail.com> wrote:
> 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 at pearwood.info> wrote:
>>
>> On Mon, Mar 26, 2018 at 01:48:34PM +0100, Cammil Taank wrote:
>> > Hi,
>> >
>> > I find a common idiom in Python is:
>> >
>> > x = x or 'some other value'
>>
>> 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
>> _______________________________________________
>> Python-ideas mailing list
>> Python-ideas at python.org
>> https://mail.python.org/mailman/listinfo/python-ideas
>> Code of Conduct: http://python.org/psf/codeofconduct/
>
>
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>


More information about the Python-ideas mailing list