473,573 Members | 2,869 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Re: Python 2.5: wrong number of arguments given in TypeError forfunction argument aggregation (dictionary input vs the norm)

On Fri, Oct 31, 2008 at 8:49 AM, mark floyd <em******@gmail .comwrote:
I was doing some testing with the different ways to pass arguments into
functions and ran into what looks like a bug.

Given function,

def foo(a,b,c):
print a
print b
print c

# Call function with named parameter list, leaving 'b' out
foo(a=1, c=3)

Traceback (most recent call last):
File "aggregate. py", line 13, in <module>
foo(a=1, c=3)
TypeError: foo() takes exactly 3 arguments (2 given)

# Call function with dictionary for parameter list... leaving 'c' out of the
dictionary
yarg = {'a': 111, 'b': 222}
foo(**yarg)

Traceback (most recent call last):
File "aggregate. py", line 17, in <module>
foo(**yarg)
TypeError: foo() takes exactly 3 non-keyword arguments (2 given)
# Call function with dictionary for parameter list... leaving 'b' out of the
dictionary

yarg = {'a': 111, 'c': 333}
foo(**yarg)

Traceback (most recent call last):
File "aggregate. py", line 17, in <module>
foo(**yarg)
TypeError: foo() takes exactly 3 non-keyword arguments (1 given)

It seems like the interpreter craps out too early when you leave 'b' out of
the input dictionary... and it reports the incorrect number of arguments
given (I would expect to see '2 given')

We've tested this locally using Python 2.5, Debian Etch 32-bit installation
Mark, this is correct behavior. You have
3 positional arguments in the function
definition. You _must_ aupply _all_ 3
of them. If you wish for b to be optional,
then you must give it a default value.

def foo(a, b=None, c=None):
print a
print b
print c

Note, that c must also be a default argument
as you cannot have a non-default argument following
a default argument.

A more useful approach is this common pattern:

def foo(*args, **kwargs):
...

What you have discovered is not a bug :)

cheers
James

--
--
-- "Problems are solved by method"
Oct 30 '08 #1
0 2297

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

Similar topics

7
3649
by: svilen | last post by:
hello again. i'm now into using python instead of another language(s) for describing structures of data, including names, structure, type-checks, conversions, value-validations, metadata etc. And i have things to offer, and to request. And a lot of ideas, but who needs them.... here's an example (from type_struct.py):
10
3672
by: Andrew Dalke | last post by:
Is there an author index for the new version of the Python cookbook? As a contributor I got my comp version delivered today and my ego wanted some gratification. I couldn't find my entries. Andrew dalke@dalkescientific.com
0
1031
by: M.-A. Lemburg | last post by:
On 2008-10-31 00:18, John Krukoff wrote: The (n given) notice refers to the number of keyword parameters you specified in the call that could be associated with a non-keyword arguments of the function. I agree that this is a bit misleading. Please open a bug report on python.org.
0
8205
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8074
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6424
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5294
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3734
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3739
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2223
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1310
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1044
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.