Skip to content

Instantly share code, notes, and snippets.

@zwgshr
Created August 26, 2017 12:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zwgshr/2f7b936fa58fdec2bc6dab1c06f25403 to your computer and use it in GitHub Desktop.
Save zwgshr/2f7b936fa58fdec2bc6dab1c06f25403 to your computer and use it in GitHub Desktop.
def hasattrs(obj, attrs):
attrs = attrs.split('.')
objs = attrs[0]
for attr in attrs[1:]:
if hasattr(obj, attr):
obj = eval('{}.{}'.format(objs, attr))
objs += '.{}'.format(attr)
else:
return False
return obj
class C():
d = 233
class B():
c = C()
class A():
b = B()
a = A()
print(hasattrs(a, 'a.b.c.d'))
print(hasattrs(a, 'a.b.e.d'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment