从类传递参数到方法pd.read_sql吗?

时间:2019-02-11 15:29:54

标签: python python-3.x

我有一个数据库接口类,该类从熊猫pd.read_sql函数返回查询。我想将pd.read_sql参数传递给方法,以便在调用函数时可以使用它,如下所示:

import pyodbc
import pandas as pd


class Database:

    def __init__():
        driver_path = 'blablabla'

    def select(self, query, **kwargs):

        con = pyodbc.connect(driver_path)
        result = pd.read_sql(query, con, kwargs)
        con.close()

        return result

但这不起作用。有没有办法像我使用R的省略号那样将参数传递给函数内部的函数? (...

例如,我想运行如下查询:

query = "Select * from Table Where Group = '?'"

Database.select(query, params='GROUP1')

1 个答案:

答案 0 :(得分:1)

您只需在**之前添加kwargs,就像在select方法中传递它的方式一样:

class Database:

    def __init__():
        driver_path = 'blablabla'

    def select(self, query, **kwargs):

        con = pyodbc.connect(driver_path)
        result = pd.read_sql(query, con, **kwargs)
        con.close()

        return result