Document.get
is a powerful call, can be used to get a document’s value against a key, and can also be used to search through internal tables in a document.
However good replacements for the same are getattr and list comprehension. Instead of writing doc.get('items')
it’s better to write doc.items
, and in place of doc.get(key)
, we can use getattr(doc, key)
. Instead of using
doc.get('items', {'rate': ['>=', 100})
we can write
[row for row in doc.items if row.rate >=100]
IMHO, the getattr and list comprehensions are better as
- They are python standards.
- They are a bit faster. doc.get is used almost everywhere. If we fix every place, we sure might a a bit of performance overhead as a function stack is getting reduced.
This should be followed as conventions as this is not a single release’s job. We can start rolling out changes in default library, and eventually start showing a deprecation warning, and finally an attribute error in a couple of years.