Python学习笔记(七)函数的使用
python中的函數(shù)使用較簡(jiǎn)單,這里列出值得注意的幾點(diǎn):
?
內(nèi)嵌函數(shù)
?
例如:
# coding: utf-8def foo():def bar():print 'bar() called.'print 'foo() called.'foo() bar()對(duì)bar的調(diào)用是非法的,因?yàn)閎ar的作用域僅限于foo內(nèi),除非使用閉包將其返回。
# coding: utf-8def foo():def bar():print 'bar() called.'print 'foo() called.'return bars = foo() s()此時(shí)對(duì)s的調(diào)用就是合法的,因?yàn)槲覀冊(cè)谕獠恳昧薭ar。
?
?
裝飾器
?
我們?cè)陬愔芯帉憇tatic函數(shù)時(shí),就使用了static包裝器,如下:
# coding: utf-8class Test():@staticmethoddef foo():print 'static method'Test.foo()實(shí)際上,包裝器就是函數(shù)。
下面看一個(gè)demo:
# coding: utf-8from time import ctimedef testfunc(func):def wrappedFunc():print '[%s] %s() called.' % (ctime(), func.__name__)return func()return wrappedFunc@testfunc def foo():print 'foo called.'foo()運(yùn)行結(jié)果如下:
5:34:25 wing@ubuntu func python 2.py [Fri Nov 14 05:36:31 2014] foo() called. foo called.上面如果不對(duì)foo添加裝飾器,還可以這樣調(diào)用:
# coding: utf-8from time import ctimedef testfunc(func):def wrappedFunc():print '[%s] %s() called.' % (ctime(), func.__name__)return func()return wrappedFunc#@testfunc def foo():print 'foo called.'foo = testfunc(foo) foo() 效果相同。?
?
函數(shù)的參數(shù)
?
在C++中,參數(shù)的位置是絕對(duì)不可以更改的,但是python中如果指定參數(shù)名,那么可以更改,例如:
# coding: utf-8def foo(a, b):print 'a = %d, b = %d' % (a, b)foo(4, 3) foo(a = 4, b = 3) foo(b = 3, a = 4)最后一行調(diào)用,a和b就更換了位置。
?
函數(shù)的缺省參數(shù)
?
# coding: utf-8def foo(a, b = 20):print 'a = %d, b = %d' % (a, b)foo(23) foo(a = 12) foo(4, 3) foo(a = 4, b = 3) foo(b = 3, a = 4)存在缺省參數(shù)時(shí),也可以指定參數(shù)名,這樣就可以調(diào)換位置
再比如:
# coding: utf-8def foo(name, age = 20, sex = 'male'):print 'name = %s, age = %d, sex = %s' % (name, age, sex)foo('zhangsan', 23, 'male') foo('lisi') foo('lisi', sex = 'female') foo(sex = 'male', name = 'gaga') foo('haha', 34) foo(age = 23, name = 'lucy') foo(sex = 'female', age = 88, name = 'wangwu')?
不帶關(guān)鍵字的可變參數(shù)
?
C語言中的printf函數(shù),可以使用可變參數(shù),意思就是參數(shù)的個(gè)數(shù)和類型是不確定的,Python同樣支持這種用法。
# coding: utf-8def foo(a, b = 'default Value', *theList):print 'a = ', aprint 'b = ', bfor i in theList:print 'other argument :', ifoo('abc') foo(23, 4.56) foo('abc', 123, 'xyz') foo('abc', 123, 'xyz', 'haha', 'gaga', 34)我們?cè)趨?shù)的最后一個(gè)位置寫入*theList,意思就是多余的參數(shù)寫入一個(gè)元組中。
注意這里的參數(shù)都是不帶關(guān)鍵字的,如果我們使用了c = 5,那么導(dǎo)致運(yùn)行錯(cuò)誤。
?
帶關(guān)鍵字的可變參數(shù)
?
如果我們真的需要使用c = 5這種額外的參數(shù),可以使用**theRest,將多余的參數(shù)放入字典。
# coding: utf-8def foo(a, b = 'default Value', **theDict):'除了a和b外,其余的參數(shù)放入'print 'a = ', aprint 'b = ', bfor eachKey in theDict.keys():print 'Other argument %s: %s' % (eachKey, theDict[eachKey])foo('abc') foo(23, 4.56) foo('abc', 123, c = 'xyz') foo('abc', 123, c = 'xyz', d = 'haha', e = 'gaga') foo(c = 'xyz', a = 'haha', b = 'gaga') foo('xyz', c = 'haha', b = 'gaga') foo('hehe', c = 'c')?
二者還可以結(jié)合使用:
# coding: utf-8def foo(a, b = 'default Value', *theList, **theDict):print 'a = ', aprint 'b = ', bfor i in theList:print 'argument :', ifor eachKey in theDict.keys():print 'Other argument %s: %s' % (eachKey, theDict[eachKey])foo('abc', 123, 'zhangsan', c = 'xyz', d = 'haha', e = 'gaga')?
對(duì)于上面的代碼,嘗試調(diào)用:
foo(2, 4, *(6, 8), **{'foo' : 12, 'bar' : 24})運(yùn)行結(jié)果為:
a = 2 b = 4 argument : 6 argument : 8 Other argument foo: 12 Other argument bar: 24 這與我們手工列出各項(xiàng)變量,結(jié)果是一致的。
注意不帶關(guān)鍵字的集合未必是元組,只要是序列就可以,于是:
foo(2, 4, *"abcdefg", **{'foo' : 12, 'bar' : 24})打印結(jié)果為:
a = 2 b = 4 argument : a argument : b argument : c argument : d argument : e argument : f argument : g Other argument foo: 12 Other argument bar: 24如果我們將元組和字典提前定義,如下:
aTuple = (6, 8) bDict = {'foo' : 12, 'bar' : 24} foo(2, 4, *aTuple, **bDict)結(jié)果與上面是一致的。
不過值得注意的是:
foo(1, 2, 3, x = 4, y = 5, *aTuple, **bDict)結(jié)果為:
a = 1 b = 2 argument : 3 argument : 6 argument : 8 Other argument y: 5 Other argument x: 4 Other argument foo: 12 Other argument bar: 24解釋器自動(dòng)幫我們進(jìn)行了合并。
?
這里我們應(yīng)該認(rèn)識(shí)到,*和**在函數(shù)調(diào)用時(shí)的作用就是講元組或者字典展開,這與C語言中的*解引用操作語義一致。
轉(zhuǎn)載于:https://www.cnblogs.com/inevermore/p/4098271.html
總結(jié)
以上是生活随笔為你收集整理的Python学习笔记(七)函数的使用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 系统测试方案如何写?
- 下一篇: 转:纯CSS实现“鼠标移过显示层”效果