Skip to content
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

Python removes negative ranges from dictionaries #103750

Closed
wnakhleh opened this issue Apr 24, 2023 · 3 comments
Closed

Python removes negative ranges from dictionaries #103750

wnakhleh opened this issue Apr 24, 2023 · 3 comments
Labels
type-bug An unexpected behavior, bug, or error

Comments

@wnakhleh
Copy link

Bug report

Python removes negative ranges from dictionaries

Example to reproduce:

print({range(16, 1): 0.5, range(32, 16): 0.5})

The expected output:

{range(16, 1): 0.5, range(32, 16): 0.5}

The actual output:

{range(16, 1): 0.5}

Your environment

  • CPython versions tested on: 3.10, 3.7.6
  • Operating system and architecture: WIN11+linux
@wnakhleh wnakhleh added the type-bug An unexpected behavior, bug, or error label Apr 24, 2023
@Eclips4
Copy link
Member

Eclips4 commented Apr 24, 2023

However, this seems interesting:

>>> a = {range(16, 1): 1}
>>> a[range(32, 16)] = 2
>>> a
{range(16, 1): 2}

@Eclips4
Copy link
Member

Eclips4 commented Apr 24, 2023

Python removes negative ranges from dictionaries

You misunderstood something. Python doesn't remove anything from dictionary.

hash(range(32, 16)) == hash(range(16, 1))
> True
# And there's need a check for objects equality 
range(32, 16) == range(16, 1)
> True

So, after these checks, Python decides to change the value of key.

@wnakhleh
Copy link
Author

Since both are empty ranges...
Thanks for the clarification

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

2 participants