Trouble understanding `content_as`

I’m trying to understand how to use the 3.0.7 Python SDK with lookup_in, but the documentation is very confusing.

The example here seems outdated (the result from lookup_in doesn’t support indexing). This also seems wrong, as LookupInResult doesn’t have a .get method.

So I end up looking at retrieving on the official page; it suggests using content_as to get the value, but what if I don’t know the content’s type for a path? How do I use it?

Hi @lcosmin,

The lookup_in operation will return a LookupInResult object. Since the lookup_in operation will allow for multiple get/exist operations, the content of the LookupInResult can be retrieve via index where the index matches the order of the get/exist operations provided in the lookup_in() call. In general, the content_as property acts as a proxy to Python’s __getitem__ method, where the SDK uses the key passed in to decode the item (i.e. content_as[str] is returning str(content)). In the case of lookup_in, since the content is indexed, the content_as property is returning a lambda that takes the index, so the correct content can be returned (i.e. content_as[str](1) is returning str(content_array[1])).

If you are retrieving a JSON document, content_as[dict] should be a safe bet. However, with subdoc operations, content_as[str] should be safe and then your app logic can be used to convert the type if needed.

I hope this helps.

Thanks. I managed to figure things out, thanks to python’s introspection. The documentation is seriously lacking though :frowning: