Python class inheritance - how to use this to connect to a mysql database
<p>I am using python 2.7 and I have written a set of python classes in order to upload data to my database. However I am not sure I am completely understanding  how to use inheritance from class to class. What I have is a User and Database class - which searches for the users/ databases from a list:</p>

<pre><code>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
</code></pre>

<p>I get my values for user and database using raw_input() which returns:</p>

<pre><code>un = 'user1'
db = 'db1'
</code></pre>

<p>To instantiate these classes as I understand it, I need to pass these values through the class, at which time I can also call the methods -</p>

<pre><code>User(un).find_user()
Database(db).find_db()
</code></pre>

<p>I now want to use a third class to inherit these values in order to connect to the database:</p>

<pre><code>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()
</code></pre>

<p>I then try and connect using:</p>

<pre><code>DatabaseConnect().connect('password1')
</code></pre>

<p>However this doesn't work. I have tried to add to my DatabaseConnect class <strong>init</strong> function:</p>

<pre><code>def __init__(self, username, database):
    User.__init__(self, username)
    Database.__init__(self, database)        
</code></pre>

<p>I have also played around with creating object variables from these classes such as:</p>

<pre><code>user_obj = User(un)
user_obj.find_user()

db_obj = Database(db)
db_obj.find_user()
</code></pre>

<p>Do I need to create these object variables and then pass them through my DatabaseConnection class - if so do I even need to inherit? This is why I am confused. Say I use these object variables and use this class:</p>

<pre><code>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()
</code></pre>

<p>and then I instantiate it using:</p>

<pre><code>db_connect = DatabaseConnect(user_obj, db_obj) 
</code></pre>

<p>How is this any different from simply using the variables themselves:</p>

<pre><code>db_connect = DatabaseConnect(un, db) 
</code></pre>

<p>and why do i have to use:</p>

<pre><code>self.username = User.username
</code></pre>

<p>instead of simply:</p>

<pre><code>self.username = username
</code></pre>

<p>I am struggling to get my head around this concept so any head would be appreciated. Thanks</p>