1

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:

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

I get my values for user and database using raw_input() which returns:

un = 'user1'
db = 'db1'

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 -

User(un).find_user()
Database(db).find_db()

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

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()

I then try and connect using:

DatabaseConnect().connect('password1')

However this doesn't work. I have tried to add to my DatabaseConnect class init function:

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

I have also played around with creating object variables from these classes such as:

user_obj = User(un)
user_obj.find_user()

db_obj = Database(db)
db_obj.find_user()

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:

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()

and then I instantiate it using:

db_connect = DatabaseConnect(user_obj, db_obj) 

How is this any different from simply using the variables themselves:

db_connect = DatabaseConnect(un, db) 

and why do i have to use:

self.username = User.username

instead of simply:

self.username = username

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

2 Answers 2

2

If you are inheriting you dont need to create a User and Database instance. You can just create a DatabaseConnect object:

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
Sign up to request clarification or add additional context in comments.

10 Comments

Ah, 14 seconds earlier
Thank you. But I'm still confused. I dont get what the point of doing this is - because without instantiating my User and Database classes I can't call the find_user() and find_database methods, right? Also, how is this any different from simply putting in my DatabaseConnect: def __init__(self, username, database): 'self.username = username self.database = database This would have the same result?
Yes you can, you now can do this dbConnect.find_db() and dbConnect.find_user() I update my answer
When you instantiate DatabaseConnect and called User and Database init you have access to these methods and properties also
I understand now! Thank you very much! So basically when you call the child class which inherits from parent classes you can now access their methods - great :D
|
0

By doing:

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

you are not properly initialising the User and Database instances. You need to call their __init__ function. There are several ways to do so e.g.:

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

after doing so you can use self.username, etc

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.