Skip to content

Instantly share code, notes, and snippets.

@eedeep
Created June 5, 2013 05:12
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/5711756 to your computer and use it in GitHub Desktop.
Save eedeep/5711756 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['nonexistent'])
except KeyError as e:
val = getattr(myobj, 'thing', None)
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