Skip to content

Instantly share code, notes, and snippets.

@fodra
Created April 21, 2017 00:42
Show Gist options
  • Save fodra/23ba85b840562e678ae5f244d1c94487 to your computer and use it in GitHub Desktop.
Save fodra/23ba85b840562e678ae5f244d1c94487 to your computer and use it in GitHub Desktop.
Found this in django-zebra a utility to get the attribute if it exists.
def _get_attr_value(instance, attr, default=None):
"""
Simple helper to get the value of an instance's attribute if it exists.
If the instance attribute is callable it will be called and the result will
be returned.
Optionally accepts a default value to return if the attribute is missing.
Defaults to `None`
>>> class Foo(object):
... bar = 'baz'
... def hi(self):
... return 'hi'
>>> f = Foo()
>>> _get_attr_value(f, 'bar')
'baz'
>>> _get_attr_value(f, 'xyz')
>>> _get_attr_value(f, 'xyz', False)
False
>>> _get_attr_value(f, 'hi')
'hi'
"""
value = default
if hasattr(instance, attr):
value = getattr(instance, attr)
if callable(value):
value = value()
return value
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment