Connecting Tech Pros Worldwide Forums | Help | Site Map

how best to use a dictionary in this function?

Terrence Brannon
Guest
 
Posts: n/a
#1: Oct 2 '08
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.

Aaron \Castironpi\ Brady
Guest
 
Posts: n/a
#2: Oct 2 '08

re: how best to use a dictionary in this function?


On Oct 2, 4:18*am, Terrence Brannon <metap...@gmail.comwrote:
Quote:
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.
Quote:
Quote:
Quote:
>>def f( **kwar ):
.... print kwar
....
Quote:
Quote:
Quote:
>>f( a=2, b=3 )
{'a': 2, 'b': 3}
Quote:
Quote:
Quote:
>>f( 123 )
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: f() takes exactly 0 arguments (1 given)
Quote:
Quote:
Quote:
>>f( a='abc' )
{'a': 'abc'}
Tim Roberts
Guest
 
Posts: n/a
#3: Oct 5 '08

re: how best to use a dictionary in this function?


Terrence Brannon <metaperl@gmail.comwrote:
Quote:
>
>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
}
Quote:
>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.
--
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.
Closed Thread