* args a python, and the difference between the 2 ** kwargs

* Args parameter is a variable length, sometimes we are not sure when to write several parameters when defining the function, you can use variable length parameters.

Such as sending a http request, some need to pass headers, cookies, and some requests do not need to, you can use a variable length parameter.

** kwargs keyword parameters, the parameters are passed dictionary format.

When connecting to the database, you need to pass the address, database name, user name, password, these parameters this time we can use the keyword parameters.

Write a configuration file, the time needed to pass directly keyword arguments.

import pymysql
from tools.read_yml import read_file
from tools.read_project_path import db_config_path


conf = read_file(db_config_path)

class DbMysql():

    def do_mysql(self,search_sql):
        conn = pymysql.connect(**conf)
        cursor = conn.cursor()
        cursor.execute(search_sql)
        res = cursor.fetchone()
        cursor.close()
        conn.close()
        return res

if __name__ == '__main__':
    print(DbMysql().do_mysql('select * from student'))

 

Guess you like

Origin www.cnblogs.com/xiamaojjie/p/11920864.html