语法:
def functionname( parameters ): "function_docstring" function_suite return [expression]
例(定义一个函数):
def printme( str ): "This prints a passed string into this function" print (str) return
调用函数:
#!/usr/bin/python
# Function definition is here
def printme( str ):
"This prints a passed string into this function"
print (str)
return;
# Now you can call printme function
printme("I'm first call to user defined function!")
printme("Again second call to the same function")
执行结果返回:
I'm first call to user defined function! Again second call to the same function
传参,往函数里面传递参数:
所有参数(参数)用Python语言通过引用传递。这意味着,如果你改变什么参数指的是在一个函数中,变化也反映在回调用函数。例如 -
#!/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
在这里,我们维持传递的对象,并在同一个对象附加的值(如:列表、元组、字典等)。因此,这将产生以下结果 -
Values inside the function: [10, 20, 30, [1, 2, 3, 4]]
Values outside the function: [10, 20, 30, [1, 2, 3, 4]]
1、普通参数:
#!/usr/bin/python
# Function definition is here
def printme( str ):
"This prints a passed string into this function"
print (str)
return;
# Now you can call printme function
printme()
当我们执行时,会得到以下返回值,因为执行函数时,没有传入必要的参数
Traceback (most recent call last):
File "test.py", line 11, in <module>
printme();
TypeError: printme() takes exactly 1 argument (0 given)
2、指定参数:
#!/usr/bin/python
# Function definition is here
def printme( str ):
"This prints a passed string into this function"
print str
return;
# Now you can call printme function
printme( str = "My string") #指定需要传入的参数,执行结果得到:My string
3、默认参数:
#!/usr/bin/python
# Function definition is here
def printinfo( name, age = 35 ):
"This prints a passed info into this function"
print ("Name: ", name)
print ("Age ", age)
return;
# Now you can call printinfo function
printinfo( age=50, name="miki" )
printinfo( name="miki" )
上面定义了传入的参数个数,其中age = 35(默认值),我们看下执行结果,当没有指定参数传入时,默认显示则为35
Name: miki
Age 50
Name: miki
Age 35
4、动态参数:
*args
**kwargs
#!/usr/bin/python
# Function definition is here
def printinfo( arg1, *vartuple ):
"This prints a variable passed arguments"
print "Output is: "
print arg1
for var in vartuple:
print var
return;
# Now you can call printinfo function
printinfo( 10 )
printinfo( 70, 60, 50 )
我们传入了一个变量,跟一个元组,执行后得到
Output is:
10
Output is:
70
60
50
5、万能参数:
*args,**kwargs
lambda语法:
lambda [arg1 [,arg2,.....argn]]:expression
#!/usr/bin/python
# Function definition is here
sum = lambda arg1, arg2: arg1 + arg2;
# Now you can call sum as a function
print "Value of total : ", sum( 10, 20 )
print "Value of total : ", sum( 20, 20 )
执行得到以下结果:
Value of total : 30
Value of total : 40
return语句:
#!/usr/bin/python
# Function definition is here
def sum( arg1, arg2 ):
# Add both the parameters and return them."
total = arg1 + arg2
print "Inside the function : ", total
return total;
# Now you can call sum function
total = sum( 10, 20 );
print "Outside the function : ", total
执行得到结果:
Inside the function : 30
Outside the function : 30
全局变量:
本地变量:
#!/usr/bin/python
total = 0; # This is global variable.
# Function definition is here
def sum( arg1, arg2 ):
# Add both the parameters and return them."
total = arg1 + arg2; # Here total is local variable.
print "Inside the function local total : ", total
return total;
# Now you can call sum function
sum( 10, 20 );
print ("Outside the function global total : ", total)
执行结果
Inside the function local total : 30
Outside the function global total : 0
本地变量声明全局变量