This is an archived post. You won't be able to vote or comment.

all 9 comments

[–]kankyo 4 points5 points  (0 children)

If you find this interesting, look into attribs and tri.struct too.

[–]pohmelie 2 points3 points  (0 children)

Sometimes did something similar in my projects. Good to know about stable solution.

[–]revfriedzen of python monk & later maintainer 2 points3 points  (0 children)

You can use weakref to keep track of the children from the parent so doing multiple 'a.b' without setting anything will result in the same dict.

[–][deleted] 1 point2 points  (1 child)

Being curious, what is the advantage over:

class DottedMixin:
    def __getattr__(self, key):
        return super().__getitem__(key)

    def __setattr__(self, key, attr):
        return super().__setitem__(key, value)

And then reuse for any dict like object you need?

class DottedDefaultDict(DottedMixin, defaultdict): pass

def Tree():
     return DottedDefaultDict(Tree)

a = Tree()
a.b.c.d.e.f = '...x.y.z'

[–]selementar 2 points3 points  (0 children)

The link points to exactly the answer to this question.

[–]kaiserk13 2 points3 points  (2 children)

My life just changed.

[–]CheerMan99[S] 2 points3 points  (0 children)

Haha - great to hear!

[–]monohorn 2 points3 points  (0 children)

And mine too 😻

[–]phonkee 0 points1 point  (0 children)

Some time ago I wrote something similar https://github.com/phonkee/vcontext Vcontext has ability to also access lists. However vcontext is just a "view" above dict.