-
-
Notifications
You must be signed in to change notification settings - Fork 62
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
Swap syntax does not work #255
Comments
You're right to doubt that this behavior can be "fixed". The current behavior is actually consistent with the relevant section of the https://docs.python.org/3/reference/simple_stmts.html docs:
So with b[1] = b[2]
b[2] = b[1] which correctly raises KeyAndValueDuplicationError on the first assignment (before it even gets to the second). Would you like to submit a PR to the https://bidict.readthedocs.io/en/main/addendum.html#caveats docs? I think that's a good suggestion. Thanks! |
Thank you for your fast meaningful reply, and for your work in To be precise, the statement
rather than
The distinction is important because when using a regular python dict
results in A casual user might expect the swap to work for Simultanous Assignment
This limitation is a direct consequence of the fact that the statement
as described in https://docs.python.org/3/reference/simple_stmts.html docs: If you agree I can submit a PR for it. Thank you again. |
Thanks for the great explanation! Please do submit the PR and I’ll take a
look ASAP. Thanks for your contribution!
…On Sat, Sep 24, 2022 at 17:43 Jorge Moraleda ***@***.***> wrote:
Thank you for your fast meaningful reply, and for your work in bidict.
To be precise, the statement b[1], b[2] = b[2], b[1] is by definition
equivalent to
newTuple = b[2], b[1]
b[1] = newTuple[0]
b[2] = newTuple[1]
rather than
b[1] = b[2]
b[2] = b[1]
The distinction is important because when using a regular python dict b[1],
b[2] = b[2], b[1] results in b being equal to {1:2, 2:1} while
b[1] = b[2]
b[2] = b[1]
results in b being equal to {1:2, 2:2}.
A casual user might expect the swap to work for bidict just as it does in
a regular dict so to document this difference between dict and bidict I
propose writing a new section at the end of *Addendum -> Caveats* with
the following content:
------------------------------
Simultanous Assignment
bidict behaves differently than dict with respect to simultaneous
assignment. For example, for a regular dict e.g. b = {1:1, 2:2} we can
swap two values with b[1], b[2] = b[2], b[1]. which results inb being
equal to {1:2, 2:1} but if b = bidict({1:1, 2:2}) then b[1], b[2] = b[2],
b[1] results in a KeyAndValueDuplicationError: (1, 2) and to swap the
values one must run:
val2 = b.pop(2)
b[1],b[2] = val2, b[1]
This limitation is a direct consequence of the fact that t the statement b[1],
b[2] = b[2], b[1] is by definition equivalent to
newTuple = b[2], b[1]
b[1] = newTuple[0]
b[2] = newTuple[1]
as described in the relevant section of the
https://docs.python.org/3/reference/simple_stmts.html docs:
------------------------------
If you agree I can submit a PR for it. Thank you again.
—
Reply to this email directly, view it on GitHub
<#255 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAAP3YFP24GJEPQWGHGAHPLV75YXPANCNFSM6AAAAAAQTOW6UE>
.
You are receiving this because you commented.Message ID:
***@***.***>
|
Documented in 22b7914. Live at https://bidict.rtfd.io/addendum.html#simultaneous-assignment |
In a regular dict, e.g.
w = {1:1, 2:2}
one can swap the values of two keys withw[1],w[2] = w[2],w[1]
but this does not work ifw
is a bidict giving the errorKeyAndValueDuplicationError: (1, 2)
.If this behavior can be fixed (which I doubt) it should, but otherwise perhaps is could be documented as a limitation and a difference with a regular dict.
By the way, as a work around (in case others arrive here with the same problem), one can simply pop the offending element before the swap as follows:
The text was updated successfully, but these errors were encountered: