def attr_chain(obj, attr):
 next = getattr(obj, attr)
 while next:
  (yield next)
  next = getattr(next, attr)

def attr_chain ( obj , attr ) : next = getattr ( obj , attr ) while next : ( yield next ) next = getattr ( next , attr )
follow an attribute chain .