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
yarg = {'a': 111, 'b': 222}

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

yarg = {'a': 111, 'c': 333}

Traceback (most recent call last):
File "aggregate. py", line 17, in <module>
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 :)


-- "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

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):
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
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.
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...
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...
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...
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...
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...
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
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
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
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.