Comments (4)
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:
Although the definition of assignment implies that overlaps between the left-hand side and the right-hand side are ‘simultaneous’ (for example
a, b = b, a
swaps two variables), overlaps within the collection of assigned-to variables occur left-to-right, sometimes resulting in confusion. For instance, the following program prints[0, 2]
:x = [0, 1] i = 0 i, x[i] = 1, 2 # i is updated, then x[i] is updated print(x)
So with b = bidict({1:1, 2:2})
, b[1], b[2] = b[2], b[1]
is by definition equivalent to
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!
from bidict.
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
_temporary_internal_tuple = b[2], b[1]
b[1] = _temporary_internal_tuple[0]
b[2] = _temporary_internal_tuple[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 the statement b[1], b[2] = b[2], b[1]
is by definition equivalent to
_temporary_internal_tuple = b[2], b[1]
b[1] = _temporary_internal_tuple[0]
b[2] = _temporary_internal_tuple[1]
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.
from bidict.
from bidict.
Documented in 22b7914.
Live at https://bidict.rtfd.io/addendum.html#simultaneous-assignment
from bidict.
Related Issues (20)
- global name 'BidirectionalMapping' is not defined HOT 21
- github git tags out of sync since 0.18.4 HOT 3
- Missing __all__ in bidict/__init__.py leads to implicit reexport error with mypy in strict mode. HOT 9
- logo HOT 5
- Dependency Dashboard
- Type hints for Bidict type HOT 2
- Try slipcover HOT 1
- Maybe use nix for devcontainer and GHA “tests” workflow
- Why it returns None when I use bidict?
- Will you add a persistence method? HOT 1
- Improve OpenSSF Scorecard HOT 6
- Automate upgrading dev dependencies HOT 1
- Type alias definition for `MISSING` results in type violations HOT 1
- test fails with python3.12 HOT 3
- Remove dead batteries
- 0.22.1: temp directory in source tree trashes build HOT 6
- 0.22.1: sphinx warnings `reference target not found` HOT 6
- mypy type inference change in 0.23.0 HOT 12
- Recommended performant alt for `namedbidict` HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from bidict.