Giter Club home page Giter Club logo

Comments (4)

jab avatar jab commented on August 17, 2024

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.

jmoraleda avatar jmoraleda commented on August 17, 2024

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.

jab avatar jab commented on August 17, 2024

from bidict.

jab avatar jab commented on August 17, 2024

Documented in 22b7914.

Live at https://bidict.rtfd.io/addendum.html#simultaneous-assignment

from bidict.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.