Python类继承-如何使用它连接到mysql数据库

Python类继承-如何使用它连接到mysql数据库,python,mysql,python-2.7,class,inheritance,Python,Mysql,Python 2.7,Class,Inheritance,我正在使用Python2.7,我已经编写了一组python类,以便将数据上传到我的数据库。然而,我不确定我是否完全理解如何在类之间使用继承。我有一个User and Database类,它从列表中搜索用户/数据库: class User(object): user_lst = ['user1', 'user2', 'user3'] def __init__(self, username): self.username = username def f

我正在使用Python2.7,我已经编写了一组python类,以便将数据上传到我的数据库。然而,我不确定我是否完全理解如何在类之间使用继承。我有一个User and Database类,它从列表中搜索用户/数据库:

class User(object):

    user_lst = ['user1', 'user2', 'user3']

    def __init__(self, username):
        self.username = username

    def find_user(self):
        if self.username not in self.user_lst:
            print('User not found, script will exit')
            exit()
        else:
            pass


class Database(object):

    db_lst = ['db1', 'db2', 'db3']

    def __init__(self, database):
        self.database = database

    def find_db(self):
        if self.database not in self.user_lst:
            print('Database not found, script will exit')
            exit()
        else:
            pass
我使用raw_input()获取用户和数据库的值,它返回:

un = 'user1'
db = 'db1'
要按照我的理解实例化这些类,我需要通过类传递这些值,在这个时候我还可以调用这些方法-

User(un).find_user()
Database(db).find_db()
现在我想使用第三个类来继承这些值,以便连接到数据库:

class DatabaseConnect(User, Database):

    def __init__(self):
        User.__init__(self, username)
        Database.__init__(self, database)

    def connect(self, pw):
        try:
            connect = MySQLdb.connect(host = 'localhost', user = self.username, passwd = pw, db = self.database, local_infile = 1)
            cursor = connect.cursor()
            print('Connected to '+self.database)
        except:
            print('Did not connect to database')
            exit()
然后,我尝试使用以下方式进行连接:

DatabaseConnect().connect('password1')
然而,这不起作用。我已尝试将以下内容添加到我的DatabaseConnect类init函数中:

def __init__(self, username, database):
    User.__init__(self, username)
    Database.__init__(self, database)        
我还尝试过从这些类创建对象变量,例如:

user_obj = User(un)
user_obj.find_user()

db_obj = Database(db)
db_obj.find_user()
我需要创建这些对象变量,然后通过DatabaseConnection类传递它们吗?如果需要,我甚至需要继承吗?这就是我感到困惑的原因。假设我使用这些对象变量并使用这个类:

class DatabaseConnect(User, Database):

    def __init__(self, username, database):
        self.username = User.username
        self.database = Database.database

    def connect(self, pw):
        try:
            connect = MySQLdb.connect(host = 'localhost', user = self.username, passwd = pw, db = self.database, local_infile = 1)
            cursor = connect.cursor()
            print('Connected to '+self.database)
        except:
            print('Did not connect to database')
            exit()
然后我使用以下方法对其进行实例化:

db_connect = DatabaseConnect(user_obj, db_obj) 
这与简单地使用变量本身有什么不同:

db_connect = DatabaseConnect(un, db) 
为什么我必须使用:

self.username = User.username
而不是简单地:

self.username = username

我正在努力让我的头脑了解这个概念,所以任何头脑都会受到赞赏。谢谢

如果要继承,则无需创建用户和数据库实例。您只需创建DatabaseConnect对象:

class DatabaseConnect(User, Database):

    def __init__(self, username, database):
        User.__init__(self, username)
        Database.__init__(self, database)

    def connect(self, pw):
        try:
            connect = MySQLdb.connect(host = 'localhost', user = self.username, passwd = pw, db = self.database, local_infile = 1)
            cursor = connect.cursor()
            print('Connected to '+self.database)
        except:
            print('Did not connect to database')
            exit()


dbConnect = new DatabaseConnect("username", "database")

dbConnect.find_db()
Database not found, script will exit

dbConnect.find_user()
User not found, script will exit
通过这样做:

def __init__(self, username, database):
    self.username = User.username
    self.database = Database.database
您没有正确初始化
用户
数据库
实例。您需要调用它们的
\uuuu init\uuu
函数。有几种方法可以做到这一点,例如:

def __init__(self, username, database):
    User.__init__(self, username)
    Database.__init__(self, database)

这样做之后,您可以使用
self.username

啊,提前14秒谢谢。但我还是很困惑。我不明白这样做的意义是什么——因为如果不实例化我的用户和数据库类,我就不能调用find_User()和find_数据库方法,对吗?另外,这与简单地放入我的数据库连接有什么不同:def uuu init_uuuu(self,username,database):'self.username=username-self.database=database这将得到相同的结果?是的,您可以,现在可以这样做dbConnect.find_db()和dbConnect.find_user()我更新我的回答当你实例化DatabaseConnect并调用User和Database init时,你可以访问这些方法和属性,我现在也明白了!非常感谢你!因此,基本上,当您调用从父类继承的子类时,您现在可以访问它们的方法-太棒了:D