def popattr(obj, attr, value=None):
    """Returns attribute `attr` from `obj` and delete it afterwards.
    If attribute does not exist, return `value`"""
    try:
        result = getattr(obj, attr)
    except AttributeError:
        return value
    else:
        delattr(obj, attr)
        return result
