Skip to content Skip to sidebar Skip to footer

How Can I Inherit From Psycopg2?

I'm trying inherit psycopg2 like this: import psycopg2 class myp(psycopg): pass ii = myp ii.connect(database = 'myDataBase', user = 'myUser', password='myPassword') Then it

Solution 1:

Disclaimer: I'm not familiar with psycopg

Update

Seems the documentation recommends to subclass psycopg2.extensions.connection. Then, connect() is a factory function that can still be used to create new connections, but you have to provide your class as a factory, again according to the docs

Full code may have to look more like (untested):

import psycopg2

class myp(psycopg2.extensions.connection):
    pass

ii = connect(connection_factory=myp,
             database = "myDataBase", user = "myUser", password="myPassword")

Update 2

With the updated approach, you're trying to build new classes with different/divergent interfaces. Often, composition is better than inheritance, see wikipedia and this question.


Post a Comment for "How Can I Inherit From Psycopg2?"