def replacing_yield(o, attr, val):
    'Context manager to temporarily replace an attribute'
    old = getattr(o, attr)
    try:
        (yield setattr(o, attr, val))
    finally:
        setattr(o, attr, old)
Context manager to temporarily replace an attribute
def replacing_yield(o, attr, val):
    
    old = getattr(o, attr)
    try:
        (yield setattr(o, attr, val))
    finally:
        setattr(o, attr, old)