Earlier quoted context omitted.
Because it saves me a lot of chars. foo['name']['attr1']['attr2']['morefuckingattr'] vs foo.name.atr1.attr2.morefuckingattr More of a personal preference.
Saving chars is often a poor reason to design a language feature.
Box: Python dictionaries with recursive dot notation access
41–50 of 124 posts
Re: Box: Python dictionaries with recursive dot notation access
#42Re: Box: Python dictionaries with recursive dot notation access
#43Re: Box: Python dictionaries with recursive dot notation access
#44A single class that: * De/serializes JSON and YAML * De/mangles, de/encodes keys * Provides automatic, expensive hashcode * Blacklists/transforms a bunch of likely keys because they conflict with reserved words * Overlays attrs (__box_heritage) * All in pretty complex code that obfuscates what you're really doing (especially to a maintainer) For the ability to avoid importing json/PyYAML and use clear key lookups? Th…
Re: Box: Python dictionaries with recursive dot notation access
#45I've worked on codebases that use a similar thing, and I have yet to see one where this wasn't a mistake. Most of the time, this probably gives you exactly what you want, but then there are times where you discover bugs in production because data you assumed is your custom class is a plain old dict, and now you're raising AttributeError all over the place. Another wart is if you are unfortunate enough to have keys th…
Re: Box: Python dictionaries with recursive dot notation access
#46A single class that: * De/serializes JSON and YAML * De/mangles, de/encodes keys * Provides automatic, expensive hashcode * Blacklists/transforms a bunch of likely keys because they conflict with reserved words * Overlays attrs (__box_heritage) * All in pretty complex code that obfuscates what you're really doing (especially to a maintainer) For the ability to avoid importing json/PyYAML and use clear key lookups? Th…
Why so negative here? I like Python a lot, and I don't write much Javascript, but one thing I wish I could do in Python is the dot notation from a dictionary. I sometimes used namedtuple as a cheap (but "immutable") class, so I can simply use dot notation when I am passing my object around my functions, instead of always stuffing the data into a dictionary.
Re: Box: Python dictionaries with recursive dot notation access
#47Earlier quoted context omitted.
Saving chars is often a poor reason to design a language feature.
Nope. It's called developer ergonomics.
Re: Box: Python dictionaries with recursive dot notation access
#48For those who just want a simple a.b instead of a['b'], use this: class Obj(): def __init__(self, d): self.__dict__ = d d = Obj({ 'a': 1, 'b': 2, }) print(d.a)
These days (Python >= 3.3) SimpleNamespace is a more concise way of doing this: from types import SimpleNamespace d = SimpleNamespace(a=1, b=2) print(d.a)
my_dict = {
'a': 1,
'b': 2
}
d = SimpleNamespace(**my_dict)
print(d.a)Re: Box: Python dictionaries with recursive dot notation access
#49Re: Box: Python dictionaries with recursive dot notation access
#50Probably not a huge issue if it's an isolated use-case. Not something I would want to see everywhere in my codebase.