-
-
Notifications
You must be signed in to change notification settings - Fork 30.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
frozenset
could be "updated" by |=
operator, is this intended?
#126783
Comments
frozendict
could be "updated" by |=
operator, is this intended?frozenset
could be "updated" by |=
operator, is this intended?
No, only the reference is updated: >>> set1 = set2 = frozenset([1, 2, 3])
>>> set2 |= {4, 5} # equivalent to set2 = set2 | {4, 5}
>>> set1
frozenset({1, 2, 3})
>>> set2
frozenset({1, 2, 3, 4, 5}) |
I don't anything wrong in this, consider: >>> i = 1234
>>> id(i)
140639472689744
>>> i += 1
>>> i
1235
>>> id(i)
140639472686640 On another hand, docs says: "The following table lists operations available for set that do not apply to immutable instances of frozenset:
Seems to be a docs issue. |
If you're interested in mutating immutable objects, please look into footguns such as ctypes 😄 I don't see anything that looks like a bug here. |
https://docs.python.org/3/reference/datamodel.html#object.__ior__:
>>> frozenset().__or__
<method-wrapper '__or__' of frozenset object at 0x7f84dbd25900>
>>> frozenset().__ior__
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'frozenset' object has no attribute '__ior__' |
Read my comment above, the same applies to |
Maybe we could have decided it could raise a |
Thanks that's really helpful. I thought
Understandable :) |
Again you guys are amazingly responsive and helpful, really appreciate that! |
Putting the breaking implementation change aside, can I ask for a documentation clarification? As the docs explicitly says "do not apply to immutable instances of frozenset" while they do work? |
They work, but those methods don't behave as documented on |
Bug report
Bug description:
The
|=
operator behaves unexpectedly when used withfrozenset
, where it creates a new instance. This can be misleading because it effectively updates it. I would expect this to trigger aTypeError
?CPython versions tested on:
3.9, 3.10, 3.11, 3.12, 3.13
Operating systems tested on:
macOS
The text was updated successfully, but these errors were encountered: