How can I conjoin this parent and child class in python?

时间:2015-10-30 22:02:44

标签: python python-3.x

class Parent:
    def __init__(self, host, data, user, password):
        self.db = MySql_Database(host, data, user, password)
        self.db.connect()
    def is_connected(self):
       return self.db.is_connected()
    def get_locations(self, query):
        if self.is_connected():
            return self.db.execute(query)
        else:
            return None
    def close(self):
        self.db.close()

class Child(Parent):
    def get_locations(self, query):
        cursor = super().grab_locations(query)
        results = []
        if cursor != None:
            for (name,lat,long) in cursor:
                locate = Location(name,lat,long)
                results.append(locate)
        return results

I'm grabbing code from a database. I'd like to convert my code into one class if possible, but I'm confused on how.

1 个答案:

答案 0 :(得分:1)

Do you mean that you want to combine the get_locations like this:

def get_locations(self, query):
    results = []
    if self.is_connected():
        cursor = self.db.execute(query)
        for (name,lat,long) in cursor:
            locate = Location(name,lat,long)
            results.append(locate)
    return results