Function arguments [was: help]

Steve Holden 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) :
> 
> prg.ev_ind_update(wf_pk_ev_ind=wf_pk_ev_ind,wf_fonction=wf_fonction,wf_nom=wf_nom)
> 
> 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(3)
9
  >>> fun(7)
21
  >>> fun(7, 4)
28
  >>> fun(a=7)
21
  >>> fun(a=3, b=12)
36
  >>> fun(b=99)
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.

regards
  Steve
-- 
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 mailing list