mutantkeyboard
mutantkeyboard

Reputation: 1714

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 name, match and market_parameters while skipping the providerID, and market_kind (since these are optional)

Keep in mind that *market_parameters will be a tuple of dicts 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?

Upvotes: 0

Views: 2621

Answers (1)

Yevhen Kuzmovych
Yevhen Kuzmovych

Reputation: 12130

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)

Upvotes: 2

Related Questions