对象的高度整合

时间:2020-02-01 16:20:27   收藏:0   阅读:65

对象的高度整合

一、没有对象

import pymysql  # 连接mysql的三方库,可以pip3 install pymysql安装


def exc1(host, port, db, charset, sql):
    conn = pymysql.connect(host, port, db, charset)
    conn.execute(sql)
    return xxx


def exc2(proc_name):
    conn = pymysql.connect(host, port, db, charsett)
    conn.call_proc(sql)
    return xxx


exc1('1.1.1.1', 3306, 'db1', 'utf-8', 'select * from t1')
exc1('1.1.1.1', 3306, 'db1', 'utf-8', 'select * from t2')
exc1('1.1.1.1', 3306, 'db1', 'utf-8', 'select * from t3')
exc1('1.1.1.1', 3306, 'db1', 'utf-8', 'select * from t4')
def exc1(sql, host='1.1.1.1', port=3306, db='db1', charset='utf-8'):
    conn = pymysql.connect(host, port, db, charset)
    conn.execute(sql)
    return xxx

exc1('select * from t1')
exc1('select * from t2')
exc1('select * from t3')
exc1('select * from t4')

二、有对象

import pymysql


class Foo:
    def __init__(self, host, port, db, chartset):
        self.host = host
        self.port = port
        self.db = db
        self.charset = chartset

    def exc1(self, sql):
        conn = pymysql.connect(self.host, self.port, self.db, self.charset)
        conn.execute(sql)
        return xxx

    def exc2(self, proc_name):
        conn = pymysql.connect(self.host, self.port, self.db, self.charsett)
        conn.call_proc(sql)
        return xxx


obj1 = Foo('1.1.1.1', 3306, 'db1', 'utf-8')
obj1.exc1('select * from t1')
obj1.exc1('select * from t2')
obj1.exc1('select * from t3')
obj1.exc1('select * from t4')

obj2 = Foo('1.1.1.2', 3306, 'db1', 'utf-8')
obj2.exc1('select * from t4')
评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!