Little bit of explanation about reduce with dictionary for anybody who are not much familiar with reduce:
Before we come to the snippet lets do a little bit of reduce function.
Reduce will apply a function of two arguments cumulatively to the items of a sequence,
from left to right, so as to reduce the sequence to a single value.
Here is the syntax:
reduce(function, sequence[, initial]) -> value
If initial is present, it is placed before the items of the sequence in the calculation,
and serves as a default when the sequence is empty.
Without initial:
>>> reduce(lambda x, y: x+y, [1, 2, 3, 4, 5])
15
>>>
smiliar to ((((1+2)+3)+4)+5)
With initial:
>>> reduce(lambda x, y: x+y, [], 1)
1
>>>
That is about list, when it comes to dictionary:
First lets check what is dict.get()
method can do :
>>> d = {'a': {'b': {'c': 'files'}}}
>>> dict.get(d,'a')
{'b': {'c': 'files'}}
>>>
So, when you put dict.get method inside reduce, this is what happens:
>>> d = {'a': {'b': {'c': 'files'}}}
{'b': {'c': 'files'}}
>>> reduce(dict.get, ['a','b','c'], d)
'files'
>>>
Which is similar to :
>>> dict.get(dict.get(dict.get(d,'a'),'b'),'c')
'files'
>>>
and when you got empty list, you will get empty dict which is the default value:
>>> reduce(dict.get, [], {})
{}
>>>
Lets come back to your snippet:
dir in your snippet != builtin dir() function, it is just a name bind to an empty dictionary.
parent = reduce(dict.get, folders[:-1], dir)
So, in the above line, folders[:-1] is just a list of directories. and dir is empty_dictionary.
Please let me know if it helps in anyway.