Home
Products
Download
Tutorials
Web Ring/Links
Contact
|
Special Case–a list with a default valueWhen
the optional parameter is a list. Its value remains from the last function call. To
prevent this, the list parameter must initialize inside the function's
body.Examples :
Call with inherit | Call without inherit | def fct(A,L=[]): L.append(A) return L >>>print fct("toto") ['toto'] >>>print fct("titi") ['toto', 'titi'] >>>print fct(3) ['toto', 'titi', 3] >>>print fct("titi",[]) ['titi'] | def fct(A,L=None): if L is None : L=[] L.append(A) return L >>>print fct("toto") ['toto'] >>>print fct("titi") ['titi'] >>>print fct(3) [3] >>>print fct("titi",[]) ['titi'] |
Function call
The ways to call a function are: - Use all parameters in they order.
- Without use all optional parameters.
- explicitly naming the parameter.
Examples : All in order | Without optional parameter | Called parameter | def fct(A,B=0,C=1) : return (A+B)*C >>> print fct(5,3,2) 16 | def fct(A,B=0,C=1) : return (A+B)*C >>> print fct(2,5) 7 | def fct(A,B=0,C=1) : return (A+B)*C >>> print fct(2,c=2) 4 >>> print fct(a=2,c=2) 4 | Get the function's documentationThe user can easily access the function documentation, if off course the programmer has coded it.
Example :
def square(A) : """Function square compute the square of a value Entry Parameter: First the Value to square. Return Value The square of the first parameter. """ return A*A >>>print square.__doc__ # __ contents 2 underscores Function square computes the square of a value Entry Parameter: First the Value to square. Return Value The square of the first parameter. >>>print square(5) 25 |
|
|