如何将对象作为参数传递给函数?

时间:2017-12-08 23:32:53

标签: python python-2.7

我有功能定义:

def setup(self, **kwargs):
    pass

并称之为:

params = {
        "advert_id": adv_id,
        "enabled": True,
        "from_hour": 2,
        "to_hour": 1,
        "interval": 0,
        "weekdays": [1]
    }

setup(params)

我试图将params作为对象传递,但它不起作用。我知道正确的方法是:

setup(advert_id=1,enabled=True,from_hour=3,to_hour=4,interval=0,weekdays=[])

无论如何,我不想枚举所有参数,并希望将其打包到对象。

1 个答案:

答案 0 :(得分:3)

在函数调用中解包params

def setup(self, **kwargs):
   pass
params = {
    "advert_id": adv_id,
    "enabled": True,
    "from_hour": 2,
    "to_hour": 1,
    "interval": 0,
    "weekdays": [1]
}

setup(**params)