Answer by Emil Stenström for How to use a dot "." to access members of...
One simple way to get dot access (but not array access), is to use a plain object in Python. Like this:class YourObject: def __init__(self, *args, **kwargs): for k, v in kwargs.items(): setattr(self,...
View ArticleAnswer by Senthil for How to use a dot "." to access members of dictionary?
I like the Munch and it gives lot of handy options on top of dot access.import munchtemp_1 = {'person': { 'fname': 'senthil', 'lname': 'ramalingam'}}dict_munch =...
View ArticleAnswer by touch my body for How to use a dot "." to access members of...
To build upon epool's answer, this version allows you to access any dict inside via the dot operator:foo = {"bar" : {"baz" : [ {"boo" : "hoo"} , {"baba" : "loo"} ] }}For instance, foo.bar.baz[1].baba...
View ArticleAnswer by Hedde van der Heide for How to use a dot "." to access members of...
Not a direct answer to the OP's question, but inspired by and perhaps useful for some.. I've created an object-based solution using the internal __dict__ (In no way optimized code)payload = {"name":...
View ArticleAnswer by Dave for How to use a dot "." to access members of dictionary?
Fabric has a really nice, minimal implementation. Extending that to allow for nested access, we can use a defaultdict, and the result looks something like this:from collections import defaultdictclass...
View ArticleAnswer by volodymyr for How to use a dot "." to access members of dictionary?
If you want to pickle your modified dictionary, you need to add few state methods to above answers:class DotDict(dict):"""dot.notation access to dictionary attributes""" def __getattr__(self, attr):...
View ArticleAnswer by nehem for How to use a dot "." to access members of dictionary?
def dict_to_object(dick): # http://stackoverflow.com/a/1305663/968442 class Struct: def __init__(self, **entries): self.__dict__.update(entries) return Struct(**dick)If one decides to permanently...
View ArticleAnswer by deepak for How to use a dot "." to access members of dictionary?
This solution is a refinement upon the one offered by epool to address the requirement of the OP to access nested dicts in a consistent manner. The solution by epool did not allow for accessing nested...
View ArticleAnswer by epool for How to use a dot "." to access members of dictionary?
You can do it using this class I just made. With this class you can use the Map object like another dictionary(including json serialization) or with the dot notation. I hope to help you:class...
View ArticleAnswer by JayD3e for How to use a dot "." to access members of dictionary?
I ended up trying BOTH the AttrDict and the Bunch libraries and found them to be way to slow for my uses. After a friend and I looked into it, we found that the main method for writing these libraries...
View ArticleAnswer by Chris Redford for How to use a dot "." to access members of...
Install dotmap via pippip install dotmapIt does everything you want it to do and subclasses dict, so it operates like a normal dictionary:from dotmap import DotMapm = DotMap()m.hello =...
View ArticleAnswer by derek73 for How to use a dot "." to access members of dictionary?
I've always kept this around in a util file. You can use it as a mixin on your own classes too.class dotdict(dict):"""dot.notation access to dictionary attributes""" __getattr__ = dict.get __setattr__...
View ArticleAnswer by Michael Jackson for How to use a dot "." to access members of...
Building on Kugel's answer and taking Mike Graham's words of caution into consideration, what if we make a wrapper? class DictWrap(object):""" Wrap an existing dict, or create a new one, and access...
View ArticleAnswer by tdihp for How to use a dot "." to access members of dictionary?
I tried this:class dotdict(dict): def __getattr__(self, name): return self[name]you can try __getattribute__ too.make every dict a type of dotdict would be good enough, if you want to init this from a...
View ArticleAnswer by pbanka for How to use a dot "." to access members of dictionary?
The language itself doesn't support this, but sometimes this is still a useful requirement. Besides the Bunch recipe, you can also write a little method which can access a dictionary using a dotted...
View ArticleAnswer by Mike Graham for How to use a dot "." to access members of dictionary?
Don't. Attribute access and indexing are separate things in Python, and you shouldn't want them to perform the same. Make a class (possibly one made by namedtuple) if you have something that should...
View ArticleAnswer by Kugel for How to use a dot "." to access members of dictionary?
Derive from dict and and implement __getattr__ and __setattr__.Or you can use Bunch which is very similar.I don't think it's possible to monkeypatch built-in dict class.
View ArticleHow to use a dot "." to access members of dictionary?
How do I make Python dictionary members accessible via a dot "."?For example, instead of writing mydict['val'], I'd like to write mydict.val.Also I'd like to access nested dicts this way. For...
View ArticleAnswer by Anant for How to use a dot "." to access members of dictionary?
Best and complete implementationfrom typing import Anyclass DotDict(dict):""" dot.notation access to dictionary attributes""" def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) for...
View ArticleAnswer by mins for How to use a dot "." to access members of dictionary?
Here is a version which creates a dict, which items are accessible either by regular indexing or by attribute name.It can be initialized from dicts and/or named arguments, e.g. my_dict =...
View Article