博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python函数
阅读量:4967 次
发布时间:2019-06-12

本文共 3683 字,大约阅读时间需要 12 分钟。

语法:

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 heredef 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 heredef 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 heredef 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 
printme(); TypeError: printme() takes exactly 1 argument (0 given)
  2、指定参数:
#!/usr/bin/python# Function definition is heredef 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 heredef 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:  mikiAge  50 Name: miki Age 35
  4、动态参数:       *args       **kwargs
#!/usr/bin/python# Function definition is heredef 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 heresum = 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 heredef 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/pythontotal = 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
  本地变量声明全局变量
 

转载于:https://www.cnblogs.com/zcx-python/p/5531286.html

你可能感兴趣的文章
本月,下一月, 上一月 的 1号, 最后一号
查看>>
C_文件包含.h文件和包含.c文件总结
查看>>
mockIto
查看>>
DIB位图(Bitmap)的读取和保存
查看>>
IOS-UITableViewStyle设置Group、Plain问题
查看>>
新建Application 报错android.app.Application cannot be cast
查看>>
jvm类加载器
查看>>
<s:submit> 指定的method方法不执行
查看>>
Java:取得当前日期一周之前/之后的日期,或者是一月之前/之后的日期
查看>>
聊一聊PV和并发、以及计算web服务器的数量的方法【转】
查看>>
thrift概述
查看>>
位图排序
查看>>
MineSweeper
查看>>
PAT乙级1031
查看>>
【T_SQL】 基础
查看>>
Triangle LOVE(拓扑排序)
查看>>
MiniProfiler
查看>>
Spring创建切面(方法名匹配切面)
查看>>
net.sf.json.JSONException: Object is null
查看>>
买股票的最佳时机
查看>>