mutantkeyboard mutantkeyboard - 1 year ago 98
Python Question

How to skip optional arguments in python, and call *args directly

I'm creating a very basic match-market solution that would be used in betting shops. I have a function that looks like this:

def create_market(name, match, providerID=str(uuid.uuid4()), market_kind=4, *market_parameters):

I want to call a function with only
while skipping the
, and
(since these are optional)

Keep in mind that
will be a dict that will be sent inside the function. I unpack it like:

for idx, data in enumerate(args):
for k, v in data.iteritems():
if 'nWays' in k:
set_value = v

When I set this dict like

market_parameters = {'nWays' : 5}

and call a function like
create_market('Standard', 1, *market_parameters)

I can't seem to get the data inside the function body.

What am I doing wrong?

Answer Source

By unpacking it like *market_parameters, you send unpacked values as a providerID (if you have more values in your dictionary then as providerID, market_kind and so on).

You probably need

def create_market(name, match, *market_parameters,
                  providerID=str(uuid.uuid4()), market_kind=4):

and call function like:

create_market('Standard', 1, market_parameters) # you don't need to unpack it.

and if you want to set providerID or market_kind then:

create_market('Standard', 1, market_parameters, providerID=your_provider_id, market_kind=your_market_kind)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download