您的位置:首页 > 编程语言 > Python开发

python 之自定义函数

2016-01-24 20:03 597 查看
最近一直被定义函数这东东而头大,直到现在还是懵懵懂懂,不是不去问题,而是对于没有一点基础,注意是一丁点基础的没有的,真不知道怎么问题,脑子有些转不过弯来了。1.什么是函数定义函数函数式:将某功能代码封装到函数中,日后便无需重复编写,仅调用函数即可面向对象:对函数进行分类和封装,让开发“更快更好更强...”函数式编程最重要的是增强代码的重用性和可读性函数的理解:相对于把一组功能,组成成一个或把一堆功能块,放在一个函数里面,然后对这个组合命名一个名称。在代码读取的时候,对于函数的内容是不执行的,但会读入内存中。例子:def mail(): n = 123 n += 1 print(n)mail() # 此时mail已经并定义为一个函数名就这么多理解吧,大多都是抄来的,嘿嘿。2.说函数,不得不说的returnreturn语句是从函数返回一个值,每个函数都要有一个返回值。函数中一定要有return返回值才是完整的函数。如果你没有定义返回值,那么会得到一个结果是None对象,而None表示没有任何值。也就是说,没有retrun这个函数第一无意义,第二没结果,会是这样吗?看看实验就知道了。
def name(x,y):

z = x+y

print(z)

def names(x,y):

z = x+y

return  z

test1 = name(1,2)

test2 = names(2,3)

print(test1)

print(test2)

[/code]执行结果:
C:\Python34\python.exe E:/Python/S12/day1/test1.py

3  #这个是test1里面有print的所有有显示,但是和test2的不同在于,他只是将结果显示到屏幕上,但对于test1自身来讲,毫无异议,值为空。

None  #注意这个是test1 print的结果

5


Process finished with exit code0

[/code]希望以上总结是对的,望各界大神,来炮弹批评、批评、批评......最后:写函数,就要有return。3.形式参数。先上例子:
def name(x,y):

z = x+y

print(z)

[/code]当我们去调用时候,可以是name(1,2),其中1,代表x,2代表y,x、y就是形式参数。这里可以有多个,在调用时候是按照顺序进行匹配的。4.默认参数默认参数是指在定义参数时候就可以给参数进行赋值,当需要对赋值的参数进行修改的时候,也可在调用的时候重新赋值,若不赋值,这时候回使用当初赋值的默认值。具体例子如下:
def show(a1,a2=100):

print(a1,a2) #这里为了打印效果,因此没有使用return

show(111)

show(111,112)

[/code]执行结果:
111 100 # show(111)结果

111 112 # show(111,112)结果


Process finished with exit code0

[/code]
这个时候,加入对于a2,没有赋值,则会打印100,若赋值了,就打印赋值的内容,把原来的值就覆盖掉了。注意:默认参数必须放在参数的最后面

5.指定参数
指定参数就可以让你随心所欲了。具体例子如下:
def show(a1,a2):

print(a1,a2)

show(a2=123,a1=999) #这样就会按照顺序进行赋值。

[/code]执行结果:
999 123


Process finished with exit code

[/code]主要看结果的顺序。6.动态参数1).带一个* 默认为元组,最少传入一个参数,个数没有限制 传入是个元素就行。
def show(*arg):

print(arg,type(arg))

show(1,22,33,44,55)

[/code]执行结果
C:\Python34\python.exe E:/Python/S12/day1/test1.py

(1, 22, 33, 44, 55) <class 'tuple'>

[/code]2)带两个参数,是传入形成字典,个数没有限制
def show(**arg):

print(arg,type(arg))

show(n1=11,n2=22,n3=33)

[/code]执行结果:
C:\Python34\python.exe E:/Python/test1.py

{'n3': 33, 'n2': 22, 'n1': 11} <class 'dict'>


Process finished with exit code0

[/code]3)下面情况下,就会同时具备元组和字典的功能。写参数的时候规则是一个星星的内容放在前面,两个星星内容放在后面,同时两者的顺序也不能变
def show(*test1,**test2):

print(test1,type(test1))

print(test2,type(test2))

show(11,22,3,n1=222,n2='ddd')

[/code]执行结果:
C:\Python34\python.exe E:/Python/test1.py

(11, 22, 3) <class 'tuple'>

{'n1': 222, 'n2': 'ddd'} <class 'dict'>


Process finished with exit code0

[/code]注意顺序相当重要,顺序不对,无法执行:
def show(*test1,**test2):

print(test1,type(test1))

print(test2,type(test2))

show(n1=222,n2='ddd',11,22,3,)

[/code]执行结果:
C:\Python34\python.exe E:/Python/test1.py

File "E:/Python/S12/day1/test1.py", line 32

show(n1=222,n2='ddd',11,22,3,)

^

SyntaxError: non-keyword arg after keyword arg


Process finished with exit code1

[/code]4)加入想提前执行赋值的内容,也可以先定义赋值内容,然后将字典或元组名直接调用即可,具体如下操作:
def show(*test1,**test2):

print(test1,type(test1))

print(test2,type(test2))

show(11,22,3,n1=222,n2='ddd')

libiao = [11,22,3]

yuanzu = (11,12,{'k1':'v1'})

zidian ={'n1':11,'n2':'ddd'}

print(type(libiao))

print(type(yuanzu))

print(type(zidian))

show(*yuanzu,**zidian)

show(*libiao,**zidian)

[/code]执行结果
C:\Python34\python.exe E:/Python/test1.py

(11, 22, 3) <class 'tuple'>

{'n2': 'ddd', 'n1': 222} <class 'dict'>

<class 'list'>

<class 'tuple'>

<class 'dict'>

(11, 12, {'k1': 'v1'}) <class 'tuple'>

{'n2': 'ddd', 'n1': 11} <class 'dict'>

(11, 22, 3) <class 'tuple'>

{'n2': 'ddd', 'n1': 11} <class 'dict'>

[/code]5)一个*和两个*在格式化中的用法
第一种用法:一个星的表达方式
s1 = "{0} is {1}"

l = ['soft','test']

result = s1.format(*l)

print(result)

[/code]两个星的表达方式
s1 = "{name} is {age}"

l = {'name':'soft', 'age':20}

result = s1.format(**l)

print(result)

[/code]

6.使用lambda简单使用。lambda只是一个表达式,函数体比def简单很多。
lambda的主体是一个表达式,而不是一个代码块。仅仅能在lambda表达式中封装有限的逻辑进去。

lambda表达式是起到一个函数速写的作用。允许在代码内嵌入一个函数的定义。

具体例子:[/b]
def f ( x ,y):

return x * y

def f(x,y):

return  x*y

d1 = f(4,5)

print(d1)

t = lambda x,y:x*y

d2 = t(4,5)

print(d2)

[/code]执行结果:
C:\Python34\python.exe E:/Python/test1.py

20

20


Process finished with exit code0

[/code]
注意:这里lambda里面包含了retrun。

来自为知笔记(Wiz)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: