how best to use a dictionary in this function?

Discussion in 'Python' started by Terrence Brannon, Oct 2, 2008.

  1. 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.
    Terrence Brannon, Oct 2, 2008
    #1
    1. Advertising

  2. On Oct 2, 4:18 am, Terrence Brannon <> wrote:
    > 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.

    >>> def f( **kwar ):

    .... print kwar
    ....
    >>> f( a=2, b=3 )

    {'a': 2, 'b': 3}
    >>> f( 123 )

    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    TypeError: f() takes exactly 0 arguments (1 given)
    >>> f( a='abc' )

    {'a': 'abc'}
    Aaron \Castironpi\ Brady, Oct 2, 2008
    #2
    1. Advertising

  3. Terrence Brannon

    Tim Roberts Guest

    Terrence Brannon <> wrote:
    >
    >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.
    --
    Tim Roberts,
    Providenza & Boekelheide, Inc.
    Tim Roberts, Oct 5, 2008
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ilias Lazaridis
    Replies:
    6
    Views:
    438
    Ilias Lazaridis
    Feb 21, 2006
  2. james_027
    Replies:
    1
    Views:
    325
    Marc 'BlackJack' Rintsch
    Aug 22, 2007
  3. Navkirat Singh
    Replies:
    6
    Views:
    3,033
    Navkirat Singh
    Jul 29, 2010
  4. Chris Rebert
    Replies:
    0
    Views:
    524
    Chris Rebert
    Jul 29, 2010
  5. Fox

    dictionary within dictionary

    Fox, Mar 8, 2005, in forum: ASP General
    Replies:
    5
    Views:
    184
    Michael D. Kersey
    Mar 13, 2005
Loading...

Share This Page