函数是一组有组织的可重用代码,用于执行单个相关操作。函数为您的应用程序提供了更好的模块化和高度的代码重用性。
如您所知,Python为您提供了许多内置函数,如print等,但是您也可以创建自己的函数。这些函数称为用户定义的函数。
def functionname( parameters ): "function_docstring" function_suite return [expression]
以下函数将字符串作为输入参数,并将其打印在标准屏幕上。
def printme( str ): "This prints a passed string into this function" print str return
定义函数只会为其命名,指定要包含在函数中的参数,并构造代码块。
一旦确定了函数的基本结构,即可通过从另一个函数或直接从Python提示符处调用它来执行它。以下是调用printme函数的示例-
#!/usr/bin/python # 函数定义 def printme( str ): "This prints a passed string into this function" print str return; # 现在你可以调用 printme 函数了 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 # 函数定义 def changeme( mylist ): "This changes a passed list into this function" mylist.append([1,2,3,4]); print "Values inside the function: ", mylist return # 现在你可以调用 changeme 函数了 mylist=[10,20,30]; changeme( mylist ); print "Values outside the function: ", mylist
在这里,无涯教程维护对传递对象的引用,并将值附加在同一对象中。因此,这将产生以下输出-
链接:https://www.learnfk.comhttps://www.learnfk.com/python/python-functions.html
来源:LearnFk无涯教程网
Values inside the function: [10, 20, 30, [1, 2, 3, 4]] Values outside the function: [10, 20, 30, [1, 2, 3, 4]]
还有一个示例,其中参数通过引用传递,并且引用在调用的函数内部被覆盖。
#!/usr/bin/python # 函数定义 def changeme( mylist ): "This changes a passed list into this function" mylist=[1,2,3,4]; # 这将在 mylist 中分配新的参考 print "Values inside the function: ", mylist return # 现在你可以调用 changeme 函数了 mylist=[10,20,30]; changeme( mylist ); print "Values outside the function: ", mylist
参数 mylist 在函数changeme中是本地的。在函数中更改mylist不会影响 mylist 。该函数什么都不做,最后将产生以下输出-
Values inside the function: [1, 2, 3, 4] Values outside the function: [10, 20, 30]
您可以使用以下类型的形式参数来调用函数:
必填参数是按正确位置顺序传递给函数的参数。在此,函数调用中的参数数量应与函数定义完全匹配。
要调用函数 printme,您肯定需要传递一个参数,否则会产生如下语法错误-
#!/usr/bin/python # 函数定义 def printme( str ): "This prints a passed string into this function" print str return; # 现在你可以调用 printme 函数了 printme()
执行以上代码后,将产生以下输出-
Traceback (most recent call last): File "test.py", line 11, in <module> printme(); TypeError: printme() takes exactly 1 argument (0 given)
关键字参数与函数调用有关。在函数调用中使用关键字参数时,调用者通过参数名称标识参数。
因为Python解释器能够使用提供的关键字来将值与参数进行匹配,所以这使您可以跳过参数或将其乱序放置。您还可以通过以下方式对 printme()函数进行关键字调用-
#!/usr/bin/python # 函数定义 def printme( str ): "This prints a passed string into this function" print str return; # 现在你可以调用 printme 函数了 printme( str="My string")
执行以上代码后,将产生以下输出-
My string
请注意,参数的顺序无关紧要。
#!/usr/bin/python # 函数定义 def printinfo( name, age ): "This prints a passed info into this function" print "Name: ", name print "Age ", age return; # 现在您可以调用 printinfo 函数 printinfo( age=50, name="miki" )
执行以上代码后,将产生以下输出-
Name: miki Age 50
默认参数是一个参数,如果在函数调用中未提供该参数的值,则该参数将采用默认值。以下示例给出了关于默认参数的想法,如果未通过默认参数,则会显示默认年龄-
#!/usr/bin/python #函数定义 def printinfo( name, age=35 ): "This prints a passed info into this function" print "Name: ", name print "Age ", age return; # 现在您可以调用 printinfo 函数 printinfo( age=50, name="miki" ) printinfo( name="miki" )
执行以上代码后,将产生以下输出-
Name: miki Age 50 Name: miki Age 35
您可能需要为函数处理的参数数量超过在定义函数时指定的参数数量。这些参数称为 variable-length 参数,在函数定义中未命名,这与必需参数和默认参数不同。
def functionname([formal_args,] *var_args_tuple ): "function_docstring" function_suite return [expression]
在变量名称之前放置一个星号(*),该变量名称包含所有非关键字变量参数的值。如果在函数调用期间未指定其他参数,则该元组将保持为空。
#!/usr/bin/python # 函数定义 def printinfo( arg1, *vartuple ): "This prints a variable passed arguments" print "Output is: " print arg1 for var in vartuple: print var return; # 现在您可以调用 printinfo 函数 printinfo( 10 ) printinfo( 70, 60, 50 )
执行以上代码后,将产生以下输出-
Output is: 10 Output is: 70 60 50
lambda 函数的语法仅包含一个语句,如下所示-
lambda [arg1 [,arg2,.....argn]]:expression
以下是显示 lambda 函数形式的示例-
#!/usr/bin/python # 函数定义 sum=lambda arg1, arg2: arg1 + arg2; # 现在您可以将 sum 作为函数调用 print "Value of total : ", sum( 10, 20 ) print "Value of total : ", sum( 20, 20 )
执行以上代码后,将产生以下输出-
Value of total : 30 Value of total : 40
语句return [expression]退出函数,可以选择将表达式传递回调用方。没有参数的return语句与return None相同。
以上所有示例均未返回任何值。您可以从函数返回值,如下所示:
#!/usr/bin/python # 函数定义 def sum( arg1, arg2 ): # 添加两个参数并返回它们。 total=arg1 + arg2 print "Inside the function : ", total return total; #现在您可以调用 sum 函数 total=sum( 10, 20 ); print "Outside the function : ", total
执行以上代码后,将产生以下输出-
Inside the function : 30 Outside the function : 30
在函数体内定义的变量具有局部作用域,而在外部定义的变量具有全局作用域。
这意味着局部变量只能在声明它们的函数内部访问,而全局变量可以在整个程序主体中被所有函数访问。调用函数时,在函数内部声明的变量将进入范围
#!/usr/bin/python total=0; # 这是全局变量。 # 函数定义 def sum( arg1, arg2 ): # 添加两个参数并返回它们。 total=arg1 + arg2; # 这里的总数是局部变量。 print "Inside the function local total : ", total return total; # 现在您可以调用 sum 函数 sum( 10, 20 ); print "Outside the function global total : ", total
执行以上代码后,将产生以下输出-
Inside the function local total : 30 Outside the function global total : 0
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)