get_in function: add example of giving a string to the keys argument
KevinXOM opened this issue · 2 comments
It is currently tempting to test get_in
like this:
get_in('x', {'x':5}) # returns 5
and conclude that this will also work:
get_in('test', {'test':5}) # actually returns None
It does not work, because 'test'
is treated as ['t','e','s','t']
. In complex dictionaries, you may actually get a value, like
get_in('xy', {'x': {'y': 5}} ) # returns 5
The documentation should probably call this out explicitly, if this is the intended behavior, perhaps by giving the 'xy' example above.
I, for one, wouldn't mind an implementation where get_in('test', {'test':5})
returns 5, but I wouldn't go so far as to say that is the right approach. I'm imagining it would facilitate doing something like this:
juxt(*map(curry(get_in), ['str1', ['str2', 'str3'], 'etc']))
You need to use get_in(['test'], {'test':5})
. The first argument is treated as a sequence of keys. Every example in the docstring uses a list as the first argument.
get_in('xy', {'x': {'y': 5}} )
happens to work because strings are sequences in Python and is equivalent to get_in(['x', 'y'], {'x': {'y':5}})
(which is the suggested/encouraged form).
I am aware of the behavior, having explicitly explained it in my issue. I am saying that the documentation should not require the reader to infer the dev's intent by reading between the lines of their examples.
If the list form is suggested/encouraged, say so in the documentation.