Skip to content

Instantly share code, notes, and snippets.

@mattbriancon
Last active August 29, 2015 13:58
Show Gist options
  • Save mattbriancon/10326968 to your computer and use it in GitHub Desktop.
Save mattbriancon/10326968 to your computer and use it in GitHub Desktop.
def hasattrs(obj, attrs):
for attr in attrs.split('.'):
if not hasattr(obj, attr):
return False
obj = getattr(obj, attr)
return True
def getattrs(obj, attrs, *args):
for attr in attrs.split('.'):
if args:
obj = getattr(obj, attr, args[0])
if obj is args[0]:
return obj
else:
obj = getattr(obj, attr)
return obj
obj = SomeObject()
getattrs(obj, 'long.chain.of.attributes', None)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment