honey-team / ufpy

Ufpy (Useful Python) - package for Python with some useful features
https://honey-team.github.io/ufpy-website
MIT License
3 stars 3 forks source link

UDict indices and keys conflict #8

Closed moontr3 closed 3 months ago

moontr3 commented 3 months ago

You can get a value from a dict by using dict[x] with either index or a key.

dict = UDict(foo=1, bar=2)
dict["foo"] # 1
dict[1] # 1
dict[-1] # 2

In inbuilt dicts, you can actually use ints as a key:

dict = {1: "foo", 2: "bar"}
dict[1] # foo
dict[2] # bar

So, if you do something like this, you will get the value by the key, not index:

dict = UDict({2: "foo", 1: "bar"})
dict[1] # bar

But what if you reeeeally want to get a value by an index, not a key? This definitely needs a fix.

I propose the following solution:

dict.get(index=1) # foo
dict.get(key=1) # bar
moontr3 commented 3 months ago

And don't forget about the default feature of the inbuilt dict.get()

bleudev commented 3 months ago

And don't forget about the default feature of the inbuilt dict.get()

Thanks for your issue. I'll fix that soon