hasattr for nested attributes

python

Solution

I know this isn't really what you want to do, but it's more pythonic anyway (if you know the names of the attributes you're looking for explicitly):

try:
   do something with object.detail.infotext
except AttributeError:
   do something else

Problem

I need something like this (pseudocode): ``` if hasattr(object, 'detail.infotext') ``` I mean I want to check if object has attribute `details` and if it has, then if `details` has a attribute named `infotext` I could do it like this: ``` if hasattr(object, 'detail'): if hasattr(object.detail, 'infotext'): do something ``` But one-liners are so much easier to read.

Original source

Related problems