说明:
使用函数可以使程序实现功能模块化,大大简洁我们的程序,这里主要讨论Python中函数的下列内容:
1.函数定义与函数参数 2.局部变量和全局变量 3.函数默认参数和关键参数 4.*Args和**Kargs
因为函数部分内容跟C语言中的很多内容都十分相似,所以会结合C语言来进行对比学习。
1.函数定义与函数参数
--基本格式1:不参参数
·定义:
def sayHi(): print "Hello!"
·调用:
>>> sayHi() Hello
--基本格式2:带一个参数
·定义:
def sayHi(name): print "Hello, %s, how are you?" % name
·调用:
>>> sayHi(‘xpleaf‘) Hello, xpleaf, how are you?
--基本格式3:多个参数
·定义:
def sayHi(name, age): print "Hello, %s, how are you?" % name print "You are %s years old." % age
·调用:
>>> sayHi(‘CL‘, 20) Hello, CL, how are you? You are 20 years old.
2.局部变量与全局变量
·看下面一个程序:
#!/usr/bin/env python age = 29 def sayHi(): age = 28 print "function age:%s" % age sayHi() print "gloable age:",age
·执行结果如下:
xpleaf@xpleaf-machine:/mnt/hgfs/Python/day3$ python fun3.py function age:28 gloable age: 29
·Python函数中,作用域的概念与C语言中是一样的,这里不再提及;
·有两点需要注意:
a.通过局部变量改变全局变量,可以在函数中使用global,如下代码:
def sayHi(): global age age = 28 print "function age:%s" % age
b.全局变量没有定义,在函数中使用global定义,相当于定义全局变量,但不建议这样使用;
4.函数默认参数和关键参数
--函数默认参数
·看下面一个程序:
#!/usr/bin/env python def users(username, group = ‘GDUT‘): mydict = {} mydict[username] = group return mydict print "default argument:", users(‘xpleaf‘) #use default argument print "full argument:", users(‘xpleaf‘, ‘BAT‘) #do not use default argument
·执行结果如下:
xpleaf@xpleaf-machine:/mnt/hgfs/Python/day3$ python default.py default argument: {‘xpleaf‘: ‘GDUT‘} full argument: {‘xpleaf‘: ‘BAT‘}
·定义函数时,参数默认赋值,在使用函数时即可以不对此类参数赋值,即为默认参数;
·使用默认参数的原则:定义函数时,非默认参数在前,默认参数在后;
·如下面即是非法的情况:
#!/usr/bin/env python def users(group = ‘GDUT‘, name): mydict = {} mydict[username] = group return mydict print "default argument:", users(‘xpleaf‘) #use default argument #编译器将无法知道‘xpleaf‘是赋给group变量还是name变量 print "full argument:", users(‘xpleaf‘, ‘BAT‘) #do not use default argument
·执行时提示错误:non-default argument follows default argument
xpleaf@xpleaf-machine:/mnt/hgfs/Python/day3$ python default.py File "default.py", line 3 def users(group = ‘GDUT‘, name): SyntaxError: non-default argument follows default argument
--函数关键参数
·在使用多个默认参数的函数中,如下程序:
#!/usr/bin/env python def users(name, age, group = ‘GDUT‘, project = ‘Python‘): print ‘‘‘name:%s age:%s group:%s project:%s‘‘‘ % (name, age, group ,project) users(‘xpleaf‘, 21)
·执行结果如下:
xpleaf@xpleaf-machine:/mnt/hgfs/Python/day3$ python default.py name:xpleaf age:21 group:GDUT project:Python
-只指定第一个默认参数
·对应的users的代码修改为:
users(‘xpleaf‘, 21, ‘BAT‘)
·执行结果如下:
xpleaf@xpleaf-machine:/mnt/hgfs/Python/day3$ python default.py name:xpleaf age:21 group:BAT project:Python
-同时指定两个默认参数
·对应的users的代码修改为:
users(‘xpleaf‘, 21, ‘BAT‘, ‘stupy‘)
·执行结果如下:
xpleaf@xpleaf-machine:/mnt/hgfs/Python/day3$ python default.py name:xpleaf age:21 group:BAT project:stupy
-只需要指定第二个默认参数
·对应的users的代码修改为:
users(‘xpleaf‘, 21, project = ‘stupy‘)
·执行结果如下:
xpleaf@xpleaf-machine:/mnt/hgfs/Python/day3$ python default.py name:xpleaf age:21 group:GDUT project:stupy
·指定第二个参数,此即为关键参数,可以不按原来参数的顺序(不指定关键参数则达不到目的);
·也可以指定多个默认参数,从而不按原来参数的位置,users代码修改如下:
users(project = ‘stupy‘, name = ‘xpleaf‘, age = 21, group = ‘BAT‘)
·执行结果如下:
xpleaf@xpleaf-machine:/mnt/hgfs/Python/day3$ python default.py name:xpleaf age:21 group:BAT project:stupy
-更清晰的例子
·程序代码如下:
#!/usr/bin/env python def func(a, b=5, c=10): print ‘a=%s, b=%s, c=%s‘ % (a, b, c) func(3, 7) func(25,c=24 ) func(c=50, a=100)
·执行结果如下:
func(3, 7) # a = 3, b=7,c=10 func(25, c=24) # a=25, b=5,c=24 func(c=50, a=100) # a=100,b=5,c=50
4.*Args和**Kargs
·在参数不确定的情况下使用*args和**kargs,前者可以输出参数对应的元组,后者可以输出参数对应的字典;
·*args演示如下:
a.程序代码
def sayHI(*args): print args sayHI(‘xpleaf‘,21,‘GDUT‘)
b.执行结果
xpleaf@xpleaf-machine:/mnt/hgfs/Python/day3$ python fun2.py (‘xpleaf‘, 21, ‘GDUT‘)
·**kargs演示如下:
a.程序代码
def sayHi2(**kargs): print kargs sayHi2(name = ‘xpleaf‘, age=21, phone = 5201314)
b.执行结果
xpleaf@xpleaf-machine:/mnt/hgfs/Python/day3$ python fun2.py {‘phone‘: 34234, ‘age‘: 29, ‘name‘: ‘Alex‘}
·由于字典是没有次序的,所以输出的顺序会跟输入的不同。
本文出自 “香飘叶子” 博客,请务必保留此出处http://xpleaf.blog.51cto.com/9315560/1696907
原文地址:http://xpleaf.blog.51cto.com/9315560/1696907