1 函数的定义:
语法:
def functionname( parameters ):
"function_docstring"
function_suite
return [expression]
具体规则如下:
-
Function blocks begin with the keyword def
followed by the function name and parentheses ( ( ) ). -
Any input parameters or arguments should be placed within these
parentheses. You can also define parameters inside these parentheses. -
The first statement of a function can be an optional statement - the documentation string of the function or docstring
. -
The code block within every function starts with a colon (:) and is indented.
-
The statement return [expression] exits a function, optionally
passing back an expression to the caller. A return statement with no
arguments is the same as
return None.
2 函数传值和传引用:
All parameters (arguments) in the Python language are passed by
reference.
It means if you change what a parameter refers to within a
function, the change also reflects back in the calling function.
#!/usr/bin/python
# Function definition is here
def changeme( mylist ):
"This changes a passed list into this function"
mylist.append([1,2,3,4]);
print ("Values inside the function: ", mylist)
return
# Now you can call changeme function
mylist = [10,20,30];
changeme( mylist );
print ("Values outside the function: ", mylist)
3 在函数中传递可变长度参数:
具体申明为:
The general syntax for a function with non-keyword variable arguments is this:
def functionname([formal_args,] *var_args_tuple ):
"function_docstring"
function_suite
return [expression]
例子:
#!/usr/bin/python
# Function definition is here
def printinfo( arg1, *vartuple ):
"This prints a variable passed arguments"
print ("Output is: ")
print ("mmmm ",arg1)
for var in vartuple:
print (var)
return;
# Now you can call printinfo function
printinfo( 10 );
printinfo( 70, 60, 50 );
输出为:
D:/>python test.py
Output is:
mmmm 10
Output is:
mmmm 70
60
50
4 匿名函数:
You can use the lambda
keyword to create small anonymous
functions. These functions are called anonymous because they are not
declared in the standard manner by using the def
keyword.
-
Lambda forms can take any number of arguments but return just one
value in the form of an expression. They cannot contain commands or
multiple expressions. -
An anonymous function cannot be a direct call to print because lambda requires an expression.
-
Lambda functions have their own local namespace and cannot access
variables other than those in their parameter list and those in the
global namespace. -
Although it appears that lambda's are a one-line version of a function, they are not equivalent to inline
statements in C or C++, whose purpose is by passing function stack allocation during invocation for performance reasons.
Syntax:
The syntax of lambda
functions contains only a single statement, which is as follows:
lambda [arg1 [,arg2,.....argn]]:expression |
Example:
Following is the example to show how lembda
form of function works:
#!/usr/bin/python |
This would produce following result:
Value of total : 30
Value of total : 40
关于python函数中是传值还是传引用,我觉得应该是全部传递引用,只是在遇到某些不能修改的数据类型的时候,会表现为何传值一样的行为。
具体参见:
http://blog.csdn.net/winterTTr/archive/2008/06/27/2590741.aspx