Say we have an arbitrary object of an unknown type, and we want to access the equally arbitrary attribute associated with this object, requested as a string name. I am looking for the best approach to get this value.
In pseudo-code / Python, the current strategy I'm considering is this:
def access_attribute(obj, attr): if type(obj) == types.FunctionType: return access_attribute(obj(), attr) elif type(obj) == types.DictType: return obj.get(attr)
A couple of issues to consider:
- What happens if the attribute does not exist on the object? I raised an exception in order.
- An attribute can be deeply nested.
I believe this is a problem that should be solved by DSL programming languages ββand interpreters, but I do not know the standard / generally accepted best practices.
The idea of ββpassing custom DSL / code stored to eval at runtime with the object was raised, but I want to avoid this particular solution and prefer Python.
source share