Last active
August 29, 2015 13:58
-
-
Save mattbriancon/10326968 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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