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

confuse with int #120

Closed
lpnueg4 opened this issue Mar 19, 2020 · 1 comment
Closed

confuse with int #120

lpnueg4 opened this issue Mar 19, 2020 · 1 comment

Comments

@lpnueg4
Copy link

lpnueg4 commented Mar 19, 2020

from addict import Dict as dd

a = dd()

a.a.a[0] = 1
a.a.a[0] = 123 # here 0 is a key
print a # {'a': {'a': {0: 123}}}

a = dd()
a.a.a = [1,2,3]
a.a.a[0] = 'asd' # here 0 is a index
print a # {'a': {'a': ['asd', 2, 3]}}

How to handle this thing? if not read context, nobody know '0' is a key or a index.

@mewwts
Copy link
Owner

mewwts commented Mar 20, 2020

Hi @lpnueg4,
Code is best read with context. In your second example, you explicitly set a.a.a to be a list, hence I don't see a problem with addict here. In fact, you could create the equivalent example with a normal dictionary

>>> a = {'a': {'a': {0: 1}}}
>>> a['a']['a'][0] = 123 # here 0 is a key
>>> print(a)
{'a': {'a': {0: 123}}}

>>> a = {'a': {'a': [1, 2, 3]}}
>>> a['a']['a'][0] = 'asd' # here 0 is an index
>>> print(a)
{'a': {'a': ['asd', 2, 3]}}

and it would be the same?

@mewwts mewwts closed this as completed Sep 12, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants