> So I've also never come across "|=" being used for this purpose.
IIRC, the JavaScript implementation of "|=" can potentially be used in the way Claudio described it, instead it's based on the truthiness of the left-hand operand rather than it being "unset". But it works in that context because "null" and "undefined" are considered falsey [1]. For example: > var value = null; > var other = 2; > value |= other; > console.log(value); 2 So it effectively works like "value | other", but also sets "value" to "other" iff "value" is falsey. When the left-hand operand is truthy, it effectively does nothing. > var value = 3; > var other = 2; > value |= other; > console.log(value); 3 Also worth noting, since "value |= other" translates to "value = value | other", it works as a bitwise OR operator; not as a catch-all for assigning a default value: > var value = null; > var other = "test"; > value |= other; > console.log(value); 0 Instead, you'd have to use the standard OR operator, like this "value = value || other" (since "||=" is invalid syntax): > var value = null; > var other = "test"; > value = value || other; > console.log(value); test FWIW, I have very rarely seen "|=" used as an operator in JS, but I've seen "value = value || other" used a decent amount. --- [1] - https://developer.mozilla.org/en-US/docs/Glossary/Falsy On Wed, Feb 26, 2020 at 6:26 PM Nick Coghlan <ncogh...@gmail.com> wrote: > > > On Thu., 27 Feb. 2020, 2:03 am Guido van Rossum, <gu...@python.org> wrote: > >> On Wed, Feb 26, 2020 at 7:43 AM Claudio Jolowicz <cjolow...@gmail.com> >> wrote: >> >>> In my experience, the expression `value |= other` is a common idiom >>> across >>> programming languages to provide a default for `value` if it is "unset". >>> >> >> Interesting. Can you point to specific examples of this? In what other >> languages have you seen this? (Not that it would make us change PEP 584, >> but if this appears common we could probably warn about it prominently in >> docs and tutorials.) >> > > I was thinking that bash scripting might be an example, but I double > checked, and that's spelled 'VAR="${$VAR:-default value}" ' > > make has 'VAR ?= "default value"' > > C# uses "??=" for null coalescence on assignment. > > So I've also never come across "|=" being used for this purpose. > > Cheers, > Nick. > >> _______________________________________________ > Python-Dev mailing list -- python-dev@python.org > To unsubscribe send an email to python-dev-le...@python.org > https://mail.python.org/mailman3/lists/python-dev.python.org/ > Message archived at > https://mail.python.org/archives/list/python-dev@python.org/message/HMKYUZP5T6HTURG46GU3L72KANB65MLQ/ > Code of Conduct: http://python.org/psf/codeofconduct/ >
_______________________________________________ Python-Dev mailing list -- python-dev@python.org To unsubscribe send an email to python-dev-le...@python.org https://mail.python.org/mailman3/lists/python-dev.python.org/ Message archived at https://mail.python.org/archives/list/python-dev@python.org/message/FOL22B6MXU65P2T3M6CCMYVZSSSWR2PO/ Code of Conduct: http://python.org/psf/codeofconduct/