Anthony Kong
Anthony Kong

Reputation: 40624

Python: inject new functions into an instance of class?

I tried to manipulate the __mro__ but it it read-only

The use case is as follow:

The Connection object created from pyodbc (a DBAPI) used to provide a property called 'autocommit'. Lately I have wrapped a SQLAlchemy db connection pool around the pyodbc for better resource management. The new db pool will return a _ConnectionFairy, a connection proxy class, which no longer exposes the autocommit property.

I would very much want to leave the thrid party code alone. So inheritance of _ConnectionFairy is not really an option (I might need to override the Pool class to change how it creates a connection proxy. For source code, please see here)

A rather not-so elegant solution is to change all occurance of

conn.autocommit = True

to

# original connection object is accessible via .connection
conn.connection.autocommit = True 

So, I would like to know if it is possible at all to inject a set of getter, setter and property to an instance of _ConnectionFairy

Upvotes: 2

Views: 7902

Answers (1)

Elalfer
Elalfer

Reputation: 5338

You can "extend" almost any class using following syntax:

def new_func(self, param):
    print param

class a:
    pass

a.my_func = new_func
b = a()
b.my_func(10)

UPDATE

If you want to create some kind of wrappers for some methods you can use getattr and setattr to save original method and replace it with your implementation. I've done it in my project but in a bit different way:

Here is an example:

class A:
    def __init__(self):
        setattr(self, 'prepare_orig', getattr(self,'prepare'))
        setattr(self, 'prepare', getattr(self,'prepare_wrapper'))

    def prepare_wrapper(self,*args,**kwargs):
        def prepare_thread(*args,**kwargs):
            try:
                self.prepare_orig(*args,**kwargs)
            except:
                print "Unexpected error:", sys.exc_info()[0]
        t = threading.Thread(target=prepare_thread, args=args, kwargs=kwargs)
        t.start()

    def prepare(self):
        pass

The idea of this code that other developer can just implement prepare method in derived classed and it will be executed in the background. It is not what you asked but I hope it will help you in some way.

Upvotes: 14

Related Questions