1.如下示例, 请用面向对象的形式优化以下代码
def exc1(host,port,db,charset):
conn=connect(host,port,db,charset)
conn.execute(sql)
return xxx
def exc2(host,port,db,charset,proc_name)
conn=connect(host,port,db,charset)
conn.call_proc(sql)
return xxx
每次调用都需要重复传入一堆参数
exc1(‘127.0.0.1’,3306,‘db1’,‘utf8’,‘select * from tb1;’)
exc2(‘127.0.0.1’,3306,‘db1’,‘utf8’,‘存储过程的名字’)
# 如下示例, 请用面向对象的形式优化以下代码
#
# def exc1(host,port,db,charset):
# conn=connect(host,port,db,charset)
# conn.execute(sql)
# return xxx
# def exc2(host,port,db,charset,proc_name)
# conn=connect(host,port,db,charset)
# conn.call_proc(sql)
# return xxx
# # 每次调用都需要重复传入一堆参数
# exc1('127.0.0.1',3306,'db1','utf8','select * from tb1;')
# exc2('127.0.0.1',3306,'db1','utf8','存储过程的名字')
#
import MySQLdb
class SqlExecute:
host = '127.0.0.1'
port = 3306
name = 'root'
password = 'zwjtest'
db = 'zwjtest'
conn = MySQLdb.Connect(host, name, password, db, port, charset='utf8')
cur = conn.cursor()
def exc1(self, sql):
exc1_cur = self.cur
exc1_cur.execute(sql)
return exc1_cur.fetchall()
def exc2(self, pro_sql):
exc2_cur = self.cur
exc2_cur.callproc(pro_sql)
exc2_conn = self.conn
exc2_conn.commit()
return exc2_cur.rowcount
if __name__ == '__main__':
name_score = SqlExecute()
print(name_score.exc1('select * from name_score'))
运行结果:
2.多重继承的执行顺序,请解答以下输出结果是什么?并解释。
class A(object):
def __init__(self):
print('A')
super(A, self).__init__()
class B