473,385 Members | 1,427 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.

argument matching question

I know this is dummy, just never saw an example of this.
I want to use the special argument matching.

A code like this:

def adder(**varargs):
sum=varargs[varargs.keys()[0]]
for next in varargs.keys()[1:]:
sum=sum+varargs[next]
return sum

print adder( "first","second",'third')
------------------------
It pop up error like this:

Traceback (most recent call last):
File "learn.py", line 7, in ?
print adder( "first","second",'third')
TypeError: adder() takes exactly 0 arguments (3 given)
How to pass arguments to a functions that use dictionary collection?
Thanks

Aug 26 '05 #1
3 1033
Learning Python wrote:
A code like this:

def adder(**varargs):
sum=varargs[varargs.keys()[0]]
for next in varargs.keys()[1:]:
sum=sum+varargs[next]
return sum

print adder( "first","second",'third')

How to pass arguments to a functions that use dictionary collection?


Like adder(foo="bar", bar="baz"), but I think you really want a function
like this:

def adder(*args):
sum = args[0]
for value in args[1:]:
sum += value
return sum
Aug 26 '05 #2
thanks, got it.
I want to test the **name option for argument matching.

Aug 26 '05 #3
Leif K-Brooks wrote:
Learning Python wrote:
A code like this:

def adder(**varargs):
sum=varargs[varargs.keys()[0]]
for next in varargs.keys()[1:]:
sum=sum+varargs[next]
return sum


For that function, call:
print adder(first=1, second=2, third=3)

A better function definition for python 2.4 would be:
def adder(**varargs):
return sum(varargs.values())
And a better function definition without using sum would be:
def adder(**varargs):
values = varargs.values()
if values:
total = values[0]
for element in values[1:]:
total += element
return total
else:
return 0

--Scott David Daniels
Sc***********@Acm.Org
Aug 26 '05 #4

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

Similar topics

4
by: Rein Anders Apeland | last post by:
Consider the following working code: #include <memory> #include <iostream> void print_ptr( const std::auto_ptr< int > & thePtr = std::auto_ptr< int >() ) {
11
by: vbgunz | last post by:
Hello all, I am just learning Python and have come across something I feel might be a bug. Please enlightenment me... The following code presents a challenge. How in the world do you provide an...
7
by: desktop | last post by:
This page: http://www.eptacom.net/pubblicazioni/pub_eng/mdisp.html start with the line: "Virtual functions allow polymorphism on a single argument". What does that exactly mean? I guess it...
11
by: tech | last post by:
Hi, I need a function to specify a match pattern including using wildcard characters as below to find chars in a std::string. The match pattern can contain the wildcard characters "*" and "?",...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: 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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.