how best to use a dictionary in this function?

  • Thread starter Terrence Brannon
  • Start date
T

Terrence Brannon

Ok, here is some code:

def calc_profit(std_clicks, vip_clicks, ad_rate=200,
upline_status=None):
payout = {}
payout_std = std_clicks * rates['std'].per_click
payout_vip = vip_clicks * rates['vip'].per_click


.... now note that std_clicks and vip_clicks are passed to the
function.

Now, I improved this function this way:

def calc_profit(std_clicks, vip_clicks, ad_rate=200,
upline_status=None):
clicks = {}
clicks['std'] = std_clicks
clicks['vip'] = vip_clicks

payout = {}
for member_type in rates:
payout[member_type] = clicks[member_type] *
rates[member_type].per_click

But it seems wasteful to have to re-bind the passed-in function args
to a dictionary in the function. I think there must be some way to
improve this code and get the dictionary built without me manually
doing it...

I know there is something like *args, or **args, but since
docs.python.org is down, I cant check.
 
A

Aaron \Castironpi\ Brady

Ok, here is some code:

def calc_profit(std_clicks, vip_clicks, ad_rate=200,
upline_status=None):
    payout = {}
    payout_std = std_clicks * rates['std'].per_click
    payout_vip = vip_clicks * rates['vip'].per_click

... now note that std_clicks and vip_clicks are passed to the
function.

Now, I improved this function this way:

def calc_profit(std_clicks, vip_clicks, ad_rate=200,
upline_status=None):
    clicks = {}
    clicks['std'] = std_clicks
    clicks['vip'] = vip_clicks

    payout = {}
    for member_type in rates:
        payout[member_type] = clicks[member_type] *
rates[member_type].per_click

But it seems wasteful to have to re-bind the passed-in function args
to a dictionary in the function. I think there must be some way to
improve this code and get the dictionary built without me manually
doing it...

I know there is something like *args, or **args, but since
docs.python.org is down, I cant check.

*args is for variable-length parameters, **args is for keyword
parameters.
.... print kwar
....Traceback (most recent call last):
{'a': 'abc'}
 
T

Tim Roberts

Terrence Brannon said:
Now, I improved this function this way:

def calc_profit(std_clicks, vip_clicks, ad_rate=200,
upline_status=None):
clicks = {}
clicks['std'] = std_clicks
clicks['vip'] = vip_clicks

You can also write it this way.
clicks = {
'std': std_clicks,
'vid': vip_clicks
}
I know there is something like *args, or **args, but since
docs.python.org is down, I cant check.

The problem with using **args is that you can no longer pass your
parameters positionally. Callers of calc_profit would have to use named
parameters. It also makes your default arguments a bit less natural.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,234
Latest member
SkyeWeems

Latest Threads

Top