import sys
import copy
import types
from inspect import isfunction

class UsingHolder:
    def __init__(self, frame, old_locals):
        self.frame = frame
        self.old_locals = old_locals
    
    def __enter__(self):
        print 'Using is entered'
    
    def __exit__(self, type, value, tb):
        for local in self.frame.f_locals.keys():
            if local not in self.old_locals:
                del self.frame.f_locals[local]
        
        print 'Using holder is deleted'

def using(o):
    frame = sys._getframe().f_back
    old_locals = copy.copy(frame.f_locals)
    
    frame.f_locals.update(o.__dict__)
    for n, f in o.__class__.__dict__.items():
        if isfunction(f):
            frame.f_locals[n] = lambda *args, **kwargs: f(o, *args, **kwargs)
    
    return UsingHolder(frame, old_locals)

class A:
    def __init__(self):
        self.i = 10
        
    def f(self, j):
        print self.i, j
        
    @staticmethod
    def g():
        pass

def do_something():
    a = A()
    with using(a):
        exec('')
        f(20)

do_something()
