support chaining when setting a nested value with __setitem__
#6
Labels
enhancement
New feature or request
good first issue
Good for newcomers
help wanted
Extra attention is needed
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 = 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 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 🙂The text was updated successfully, but these errors were encountered: