赞
踩
本文主要介绍一个 Python 中函数 Function 的基础知识点,更多 Python 基础系列文章,请参考 Python 基础系列大纲
内容提要:
函数是一组用于实现某一特殊功能按一定逻辑组织的代码集合。

内建 Built-in 函数:
len(), dir(), help(), type(), str(),…

导入的模块 module 函数 :

Lambda functions
表达式函数
不用指明函数名

用户自定义函数


这 4 类参数必须按顺序在函数中申明,传递。




通过形参与实参在函数中位置来进行区分,两者区别如下:
一、主体不同
1、实参:在调用有参函数时,函数名后面括号中的参数为“实际参数”。
2、形参:不是实际存在变量,又称虚拟变量。
二、目的不同
1、实参:可以是常量、变量或表达式, 无论实参是何种类型的量,在进行函数调用时,都必须具有确定的值, 以便把这些值传送给形参。
2、形参:定义函数名和函数体的时候使用的参数,目的是用来接收调用该函数时传入的参数。
三、特点不同
1、实参:在调用函数过程中,系统会把实参的值传递给被调用函数的形参。或者说,形参从实参得到一个值。该值在函数调用期间有效,可以参加该函数中的运算。
2、形参:形参的本质是一个名字,不占用内存空间。


全局变量在函数内部是可见的:

如果一个全局变量是一个可变类型 (如:list),那么在函数内部改变它的值将会更改它更改它原始的值。

如果一个全局变量是一个不可变类型 (如:int, tuple), 即使作为参数传递给函数,它是不可更改的,将产生 Error。


对于简单类型的全局变量(如 int),在函数内部只是作为局部变量处理,不会改变其原始值。

可变参数作为默认参数:

下面这个例子重写了 build-in 变量 type 和全局变量 gx
但是 gx 在函数内部只是做为局部变量使用。

函数如果能访问一个 list 变量(通常是引用传递),就可以更改这个 list,任何在函数内部对 list 的更改都是永久的。
为了防止函数更改 list,以拷贝的方式传递给函数,而不是引用传递。

举例:
传入x[:]是x[]的一个copy,另外一个[]变量,跟原本的x[]是没有关系的,所以在函数中的parameter只是个local变量,不会影响到global变量x
x = [5]
def f(x):
x[0] += 1
f(x[:])
print(str(x)) # output:[5]
下面这个例子很好的说明 拷贝传递和引用传递的区别:

代码:
def f_args (*args): """ Function with arbitrary number of arguments passed during function call. \t Scoping: immutable object (a and c) did not change after function call but \t mutable list object (b) gets changed on not depending on whether \t it is passed by reference as b or by copy as b[:] """ print ("\tInside the function: type of args = {}".format(type (args))) la, lb = args # this is known as tuple unpacking la = 7 # la is local variable; original a-value will not change lb[1] = 5 # list is mutable: original b-value might change or not # depending on whether b is passed by reference or by copy print ("\tInside the function: b = {} and a = {:d}".format (b, a)) print ("\tInside the function: Locals = {}". format (locals())) a = 1 b = [1,2,3] # print("Before the function call: Locals = {}". format (locals())) print("Before function call: b = {} and a = {:d}".format(b, a)) # passing b by copying its values: only copy but not the original will be changed f_args (a, b [ : ]) print("After 1st function call: b = {} and a = {:d}".format(b, a)) # passing b by reference: original b will be changed by the function f_args (a, b) print("After 2nd function call: b = {} and a = {:d}".format(b, a)) # help(f_args)
def format_name(first, last):
full_name = first.title() + ' ' + last.title()
print(type(format_name ('nagiza', 'samatova')))
print(format_name ('nagiza', 'samatova'))
#output:
<class 'NoneType'>
None
返回一个简单值

return 多个值,将多个值打包成 tuple 传出
def test(x):
y1 = x+1
y2 = x+2
return y1,y2
result = test(2)
print(type(result)) #output:<class 'tuple'>
print(result) #output:(3, 4)


例如:
def do_nothing():
"""the function that does nothing
with zero input parameter
and zero return output values """
pass
help(do_nothing)
Help on function do_nothing in module __main__:
do_nothing()
the function that does nothing
with zero input parameter
and zero return output values
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。