Comment

Saverio Trioni

Why not a simple lazy object?

from django.utils.functional import SimpleLazyObject

def debug_info(request):
    return {
        'on_dev_server': SimpleLazyObject(lambda: request.get_host() in settings.DEV_HOSTNAMES),
    }

The lambda won't be executed until needed and then the result will be inserted in the object's dlegation chain instead of the lambda.

>>> s = SimpleLazyObject(lambda: True)
>>> s
<SimpleLazyObject: <function <lambda> at ...>>
>>> if s: print 8
...
8
>>> s
<SimpleLazyObject: True>
>>>

>>> s = SimpleLazyObject(lambda: False)
>>> s
<SimpleLazyObject: <function <lambda> at ...>>
>>> if s: print 8
...
>>> s
<SimpleLazyObject: False>
>>>