473,385 Members | 1,893 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

how best to use a dictionary in this function?

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.
Oct 2 '08 #1
2 871
On Oct 2, 4:18*am, Terrence Brannon <metap...@gmail.comwrote:
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'}
Oct 2 '08 #2
Terrence Brannon <me******@gmail.comwrote:
>
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, ti**@probo.com
Providenza & Boekelheide, Inc.
Oct 5 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: brianobush | last post by:
# # My problem is that I want to create a # class, but the variables aren't known # all at once. So, I use a dictionary to # store the values in temporarily. # Then when I have a complete set, I...
1
by: john wright | last post by:
I have a dictionary oject I created and I want to bind a listbox to it. I am including the code for the dictionary object. Here is the error I am getting: "System.Exception: Complex...
4
by: Betina Andersen | last post by:
I have a dictionary object, then I create a new dictionary object and sets it equal to my original, then I pass the new dictionary object to a function that changes some of my values - but then my...
8
by: akameswaran | last post by:
I wrote up a quick little set of tests, I was acutally comparing ways of doing "case" behavior just to get some performance information. Now two of my test cases had almost identical results which...
5
by: blisspikle | last post by:
I figure that someone good at dotnet can look at this and give me a clue on how to easily organize this code? If there is a unique identifier like "Publisher" with a bunch of "Book" that are...
29
by: calvert4rent | last post by:
I need to some sort of data type that will hold a listing of ID's and their counts/frequency. As I do some processing I get an ID back which I need to store and keep an accurate count for how many...
3
by: mk | last post by:
Hello everyone, I'm storing functions in a dictionary (this is basically for cooking up my own fancy schmancy callback scheme, mainly for learning purpose): .... return "f2 " + arg .......
2
by: John O'Hagan | last post by:
Hi Pythonistas, I'm looking for the best way to pass an arbitrary number and type of variables created by one function to another. They can't be global because they may have different values...
0
by: John O'Hagan | last post by:
On Tue Sep 30 11:32:41 CEST 2008, Steven D'Aprano Thanks, both to you and Bruno for pointing this out, I'll certainly be using it in future.
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.