el_pup_le
el_pup_le

Reputation: 12179

How to pass dynamic amount of optional parameters to Python function

I have a function that is being used with 2 optional parameters offset and limit:

query = db.engine.execute(sql, offset=pagination.offset, limit=pagination.limit)

Is there a way I can pass it a list of dynamic parameters? Something like this:

db.engine.execute(sql, param_obj)

Upvotes: 0

Views: 500

Answers (1)

Sunitha
Sunitha

Reputation: 12015

You can build the optional parameters as a dict and pass it to the function

param_obj = dict(offset=pagination.offset, limit=pagination.limit)
db.engine.execute(sql, **param_obj)

Or if you really want to pass the optional paraemters as list, you can, but care should be taken to ensure the order of parameters being passed

param_obj = [pagination.offset, pagination.limit]
db.engine.execute(sql, *param_obj)

Upvotes: 3

Related Questions