How to increment d['a']['b']['c'][1][2][3] if d is defaultdict of defaultdict without code dublication?
from collections import defaultdict
nested_dict_type = lambda: defaultdict(nested_dict_type)
nested_dict = nested_dict_type()
# incrementation
if type(nested_dict['a']['b']['c']['d'][1][2][3][4][5][6]) != int:
    nested_dict['a']['b']['c']['d'][1][2][3][4][5][6] = 0
nested_dict['a']['b']['c']['d'][1][2][3][4][5][6] += 1  # ok, now it contains 1
Here we can see that we duplicated (in the code) a chain of keys 3 times.
Question: Is it possible to write a function inc that will take nested_dict['a']['b']...[6] and do the same job as above? So:
def inc(x):
    if type(x) != int:
        x = 0
    x += 1
inc(nested_dict['a']['b']['c']['d'][1][2][3][4][5][6])  # ok, now it contains 1
Update (20 Aug 2018):
There is still no answer to the question. It's clear that there are options "how to do what I want", but the question is straightforward: there is "value", we pass it to a function, function modifies it. It looks that it's not possible. Just a value, without any "additional keys", etc. If it is so, can we make an answer more generic?
Notes:
- What is defaultdict of defaultdicts - SO.
- This question is not about "storing of integers in a defaultdict", so I'm not looking for a hierarchy of defaultdicts with an int type at the leaves.
- Assume that type (intin the examples) is known in advance / can be even parametrized (including the ability to perform+=operator) - the question is how to dereference the object, pass it for modification and store back in the context of defaultdict of defaultdicts.
- Is the answer to this question related to the mutability? See example below:
Example:
def inc(x):
    x += 1
d = {'a': int(0)}
inc(d['a'])  
# d['a'] == 0, immutable
d = {'a': Int(0)}
inc(d['a'])  
# d['a'] == 1, mutated
Where Int is:
class Int:
    def __init__(self, value):
        self.value = value
    def __add__(self, v):
        self.value += v
        return self
    def __repr__(self):
        return str(self.value)
 
     
    