Welcome, guest | Sign In | My Account | Store | Cart
import contextlib

def temp_setattr(ob, attr, new_value):
    """Temporarily set an attribute on an object for the duration of the
    context manager."""
    replaced = False
    old_value = None
    if hasattr(ob, attr):
            if attr in ob.__dict__:
                replaced = True
        except AttributeError:
            if attr in ob.__slots__:
                replaced = True
        if replaced:
            old_value = getattr(ob, attr)
    setattr(ob, attr, new_value)
    yield replaced, old_value
    if not replaced:
        delattr(ob, attr)
        setattr(ob, attr, old_value)