Function arguments [was: help]
steve at holdenweb.com
Fri Apr 13 14:44:24 CEST 2007
pierre-yves guido wrote:
> hello (I hope my english is not so bad),
Your English is quite good. In future, though, please try to make your
subject line say a bit more about the problem - we *all* need help!
> I'm doing a training course and I'm a newbie in Python. My problem :
> I have a form, and when I click, I make an update. But all the parameters
> are all required to make the update. So I'd like to put in my code something
> like [optional]...
> My code (simplyfied) :
> and so, when I put nothing in wf_nom, it put me that error :"the parameter
> wf_nom...was omitted from the request...". But sometimes, wf_nom is not
> required !
> Thanks to help a poor young boy
When you define a function or a method (using the def statement) you can
specify default values for arguments. A simple example:
>>> def fun(a, b=3):
... return a*b
>>> fun(7, 4)
>>> fun(a=3, b=12)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: fun() takes at least 1 non-keyword argument (0 given)
You can see several things here:
1. When you call a function you do not *have* to give thte names of the
arguments - they can be matched by position.
2. When you define a function you can specify a default value for one or
more arguments (these argument have to appear *after* the ones for which
no default is defined)
3. If you don't provide a value for an argument that has no default then
you will receive an exception, which normally results in a traceback.
Hope this helps. Welcome to Python, and its friendly community.
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
Recent Ramblings http://holdenweb.blogspot.com
More information about the Python-list