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

DefaultMunch TypeError #71

Closed
hginzel opened this issue Feb 11, 2021 · 2 comments
Closed

DefaultMunch TypeError #71

hginzel opened this issue Feb 11, 2021 · 2 comments

Comments

@hginzel
Copy link

hginzel commented Feb 11, 2021

The defaultdict example from its documentation produces TypeError

from munch import DefaultMunch
#from collections import defaultdict

import sys;     print(sys.version)
import munch;   print(munch.__version__)

s = [('yellow', 1), ('blue', 2), ('yellow', 3), ('blue', 4), ('red', 1)]
#d = defaultdict(list)
d = DefaultMunch(list)
for k, v in s: d[k].append(v)

print(sorted(d.items()))

outputs

3.8.5 (default, Jul 28 2020, 12:59:40)
[GCC 9.3.0]
2.3.2
Traceback (most recent call last):
  File "test_DefaultMunch.py", line 10, in <module>
    for k, v in s: d[k].append(v)
TypeError: descriptor 'append' for 'list' objects doesn't apply to a 'int' object
@d1618033
Copy link

d1618033 commented Feb 11, 2021

DefaultMunch only works for constants, not functions/classes. You should use DefaultFactoryMunch instead:

 from munch import DefaultFactoryMunch
 #from collections import defaultdict

 import sys;     print(sys.version)
 import munch;   print(munch.__version__)

 s = [('yellow', 1), ('blue', 2), ('yellow', 3), ('blue', 4), ('red', 1)]
 #d = defaultdict(list)
 d = DefaultFactoryMunch(list)
 for k, v in s: d[k].append(v)

 print(sorted(d.items()))

output:

3.8.6 (default, Jan  4 2021, 19:49:33)
[Clang 12.0.0 (clang-1200.0.32.27)]
2.5.0
[('blue', [2, 4]), ('red', [1]), ('yellow', [1, 3])]

@hginzel
Copy link
Author

hginzel commented Feb 13, 2021

Thank you.

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