Skip to content

Instantly share code, notes, and snippets.

@eedeep
Created June 5, 2013 05:14
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 eedeep/5711760 to your computer and use it in GitHub Desktop.
Save eedeep/5711760 to your computer and use it in GitHub Desktop.
mydict = {'mykey': 'foo', }
class MyObj(object):
thing = 'whatever'
myobj = MyObj()
val = None
try:
val = getattr(myobj, 'thing', mydict.get('nonexistent', None))
except KeyError as e:
if not val:
raise e
print val
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment