Python学习笔记(七)函数的使用
python中的函數使用較簡單,這里列出值得注意的幾點:
?
內嵌函數
?
例如:
# coding: utf-8def foo():def bar():print 'bar() called.'print 'foo() called.'foo() bar()對bar的調用是非法的,因為bar的作用域僅限于foo內,除非使用閉包將其返回。
# coding: utf-8def foo():def bar():print 'bar() called.'print 'foo() called.'return bars = foo() s()此時對s的調用就是合法的,因為我們在外部引用了bar。
?
?
裝飾器
?
我們在類中編寫static函數時,就使用了static包裝器,如下:
# coding: utf-8class Test():@staticmethoddef foo():print 'static method'Test.foo()實際上,包裝器就是函數。
下面看一個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()運行結果如下:
5:34:25 wing@ubuntu func python 2.py [Fri Nov 14 05:36:31 2014] foo() called. foo called.上面如果不對foo添加裝飾器,還可以這樣調用:
# 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() 效果相同。?
?
函數的參數
?
在C++中,參數的位置是絕對不可以更改的,但是python中如果指定參數名,那么可以更改,例如:
# 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)最后一行調用,a和b就更換了位置。
?
函數的缺省參數
?
# 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)存在缺省參數時,也可以指定參數名,這樣就可以調換位置
再比如:
# 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')?
不帶關鍵字的可變參數
?
C語言中的printf函數,可以使用可變參數,意思就是參數的個數和類型是不確定的,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)我們在參數的最后一個位置寫入*theList,意思就是多余的參數寫入一個元組中。
注意這里的參數都是不帶關鍵字的,如果我們使用了c = 5,那么導致運行錯誤。
?
帶關鍵字的可變參數
?
如果我們真的需要使用c = 5這種額外的參數,可以使用**theRest,將多余的參數放入字典。
# coding: utf-8def foo(a, b = 'default Value', **theDict):'除了a和b外,其余的參數放入'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')?
二者還可以結合使用:
# 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')?
對于上面的代碼,嘗試調用:
foo(2, 4, *(6, 8), **{'foo' : 12, 'bar' : 24})運行結果為:
a = 2 b = 4 argument : 6 argument : 8 Other argument foo: 12 Other argument bar: 24 這與我們手工列出各項變量,結果是一致的。
注意不帶關鍵字的集合未必是元組,只要是序列就可以,于是:
foo(2, 4, *"abcdefg", **{'foo' : 12, 'bar' : 24})打印結果為:
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)結果與上面是一致的。
不過值得注意的是:
foo(1, 2, 3, x = 4, y = 5, *aTuple, **bDict)結果為:
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解釋器自動幫我們進行了合并。
?
這里我們應該認識到,*和**在函數調用時的作用就是講元組或者字典展開,這與C語言中的*解引用操作語義一致。
轉載于:https://www.cnblogs.com/inevermore/p/4098271.html
總結
以上是生活随笔為你收集整理的Python学习笔记(七)函数的使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 系统测试方案如何写?
- 下一篇: Python+PyQt 数据库基本操作