deriving MySQLdb class

Sean DiZazzo half.italian at gmail.com
Thu Jan 21 22:42:35 EST 2010


On Jan 21, 5:48 pm, tekion <tek... at gmail.com> wrote:
> All,
> I am trying to write a class which inherits from MySQLdb class.  Below
> is code snippet:
> import MySQLdb
> import sys
>
> class  msql_connect(MySQLdb):
>     def __init__(self):
>         self.host     =  "hostname"
>         self.user     = "user"
>         self.password  = "passoword"
>         self.database = "database name"
>
> I am running into below error:
>  class  msql_connect(MySQLdb):
> TypeError: Error when calling the metaclass bases
>     module.__init__() takes at most 2 arguments (3 given)
>
> Does any one have an idea why?  Thanks.

MySQLdb is the name of the module, not the class you want to
subclass.  But MySQLdb.connect() is not the class either...it's a
factory function that returns instances of the class you actually want
to subclass...connections.Connection().  The below works for me.

from MySQLdb import connections
import sys

class  mysql_connect(connections.Connection):
    def __init__(self):
        self.host     =  "host"
        self.user     = "user"
        self.password  = "password"
        self.database = "database"
        connections.Connection.__init__(self, host=self.host,
user=self.user, passwd=self.password, db=self.database)

p = mysql_connect()

~Sean



More information about the Python-list mailing list