Is your feature request related to a problem? Please describe.
I want to support chaining when setting a nested value in a non-existent path. It should create all intermediate paths as needed.
Describe the solution you'd like
I want to add support for a "chaining" approach with __setitem__ as is inspired by this question, so for example:
from dotwiz import DotWiz
obj = DotWiz()
obj['a.b.c.d'] = 1
should then work, and essentially perform the equivalent of:
obj = ✫(a=✫(b=✫(c=✫(d=1))))
And maybe it makes sense to add a fallback method, set, so that can be used for cases where we don't actually desire that behavior. So in the example above,
should result in the following value of obj:
Describe alternatives you've considered
Actually, it may also be worth it to implement a __missing__ method, as outlined in this question as well. I felt like this approach was rather clever and worthwhile to look into.
Additional context
See also this post on SO for more details.
Essentially, supporting the equivalent of dw.a.b.c.d = 1 I think is not a good idea, because it involves implementing a __getattr__ apparently, which will definitely hurt performance to read/access values, since in the default implementation we don't override the builtin object.__getattr__ method definition at all.
So I want to at the very least, support this slightly different format:
I think this is a good idea, relatively speaking, because we'll only need to update the __setitem__ method, and not touch other methods like __getitem__ for example -- at least not at this point, anyway 🙂
Is your feature request related to a problem? Please describe.
I want to support chaining when setting a nested value in a non-existent path. It should create all intermediate paths as needed.
Describe the solution you'd like
I want to add support for a "chaining" approach with
__setitem__as is inspired by this question, so for example:should then work, and essentially perform the equivalent of:
And maybe it makes sense to add a fallback method,
set, so that can be used for cases where we don't actually desire that behavior. So in the example above,should result in the following value of
obj:Describe alternatives you've considered
Actually, it may also be worth it to implement a
__missing__method, as outlined in this question as well. I felt like this approach was rather clever and worthwhile to look into.Additional context
See also this post on SO for more details.
Essentially, supporting the equivalent of
dw.a.b.c.d = 1I think is not a good idea, because it involves implementing a__getattr__apparently, which will definitely hurt performance to read/access values, since in the default implementation we don't override the builtinobject.__getattr__method definition at all.So I want to at the very least, support this slightly different format:
I think this is a good idea, relatively speaking, because we'll only need to update the
__setitem__method, and not touch other methods like__getitem__for example -- at least not at this point, anyway 🙂