Skip to content

operator.rst mentioning or_ should also mention dict merge operators #141623

Closed

Description

@doronbehar

I was designing the following:

from functools import reduce
from operator import or_

dicts = [
    {"a": 1, "b": 2},
    {"c": 99, "d": 3},
    {"e": 4}
]

merged = reduce(or_, dicts, {})
print(merged)

And from reading:

| Bitwise Or | ``a | b`` | ``or_(a, b)`` |

And:

.. function:: or_(a, b)
__or__(a, b)
Return the bitwise or of *a* and *b*.

I wasn't sure if or_ applies also to dictionaries, but according to the trivial implementation, of course it does :). Anyway, I thought it would be nice to rephrase the above 2 parts of the document somehow - not sure exactly how to do phrase it myself.

Linked PRs

Activity

  1. added
    docs
    on Nov 16, 2025
  2. skirpichev commented on Nov 16, 2025

    @skirpichev
    Member

    Good catch. This affects also e.g. and_ and xor. (Maybe more.) These operators are defined e.g. for sets.

    not sure exactly how to do phrase it myself.

    I think something like "Return a | b" is fine.

  3. picnixz commented on Nov 16, 2025

    @picnixz
    Member

    Is the issue about "bitwise or" not being defined for dictionaries in this case? and that you want to have another word for this?

  4. Yashp002 commented on Nov 16, 2025

    @Yashp002
  5. picnixz commented on Nov 16, 2025

    @picnixz
    Member

    Your analysis is inccorrect. It is defined for dictionaries.

  6. Yashp002 commented on Nov 16, 2025

    @Yashp002
    Contributor

    So is the main problem here that bitwise or isn't communicated in the docs/isn't immediately obvious to be functional for dictionary merging as well?

  7. skirpichev commented on Nov 16, 2025

    @skirpichev
    Member

    Is the issue about "bitwise or" not being defined for dictionaries in this case?

    No. The issue is that sphinx docs for operator.or_ says:

    Return the bitwise or of a and b.

    It's not true. This might return a set union, or a logical or of symbolic expressions in some CAS, or something else. Docstring seems correct here:

    >>> help(operator.or_)
    Help on built-in function or_ in module _operator:
    
    or_(a, b, /)
        Same as a | b.
    

    It's just a function analog of the corresponding expression. Same for and_, xor_, invert, truediv. Even for mul.

    We might note, that for integers that means the bitwise or, or that for numbers operator.mul means multiplication. But I'm not sure if this worth to mention.

  8. picnixz commented on Nov 16, 2025

    @picnixz
    Member

    No. The problem is likely that "bitwise or" is usually used for integers, not for objects in general. Here, it would be better to talk about "binary or" in contrast to "logical or" (which we cannot override).

  9. added
    3.13
    3.14
    on Nov 16, 2025
  10. skirpichev commented on Nov 16, 2025

    @skirpichev
    Member

    The problem is likely that "bitwise or" is usually used for integers, not for objects in general.

    SymPy uses __or__() to represent logical or for expressions.

  11. picnixz commented on Nov 16, 2025

    @picnixz
    Member

    Urgh, I would have preferred the logical or to reflect the "logical" or in the sense of Python (with the or keyword and truthiness) and binary or for whatever else (binary since there are 2 operands). But if SymPy uses __or__ for logical expressions as well, let's just stick to a | b.

  12. removed
    3.13
    on Nov 16, 2025
  13. 1 remaining item

  14. picnixz commented on Nov 16, 2025

    @picnixz
    Member

    (@skirpichev no need for the labels if they affect all stable & in-dev versions)

  15. Yashp002 commented on Nov 16, 2025

    @Yashp002
    Contributor

    So the fix is to change "Return the bitwise or of a and b" to something like:

    "Return the result of a | b."

    This avoids the confusion since "bitwise" is too specific, and we let the | operator speak for itself (works for ints, sets, dicts, etc. based on their __or__ implementation).
    Would that be satisfactory?

  16. picnixz commented on Nov 22, 2025

    @picnixz
    Member

    I think so (?) at least I think it's sufficient. People don't care what the operator is named from a theoretical PoV. Strictly speaking it is a "binary operator" (in the mathematical sense) but we don't need to be picky here. a | b will be sufficient.

  17. added a commit that references this issue on Nov 22, 2025
    0dafb96
  18. doronbehar commented on Nov 22, 2025

    @doronbehar
    ContributorAuthor
  19. added 2 commits that reference this issue on May 30, 2026
    883d26d
    ff31c16
  20. added a commit that references this issue on Jun 8, 2026
  21. added a commit that references this issue on Jun 8, 2026
  22. added 2 commits that reference this issue on Jun 8, 2026
  23. added a commit that references this issue on Jun 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    docs

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions