1. 函數(shù)定義與調(diào)用
def MyFirstFunction():
print('這是我創(chuàng)建的第一個函數(shù)')
#調(diào)用
MyFirstFunction()
這是我創(chuàng)建的第一個函數(shù)
2. 函數(shù)文檔
def MySecondFunction(name):
#下面這個字符串即為函數(shù)文檔
'函數(shù)定義過程中的name叫形參'
print('你的名字是' + name)
MySecondFunction('Nigream')
#兩個雙下劃線表示系統(tǒng)屬性
print(MySecondFunction.__doc__)
#也可以用
help(MySecondFunction)
你的名字是Nigream
函數(shù)定義過程中的name叫形參
Help on function MySecondFunction in module __main__:
MySecondFunction(name)
函數(shù)定義過程中的name叫形參
?
3. 關(guān)鍵字參數(shù)
def SaySome(name , words):
print(name + '-->' + words)
SaySome(name='Nigream',words='hello')
Nigream-->hello
4. 默認參數(shù)
def SaySome(name = 'Nigream', words = 'hello'):
print(name + '-->' + words)
SaySome()
SaySome('蒼井空')
SaySome('蒼井空' , '我脫光衣服躺在鏡頭前,是為了生存;而你衣冠楚楚地站在鏡頭前贾节,卻是為了私欲和欺騙蝌麸!')
Nigream-->hello
蒼井空-->hello
蒼井空-->我脫光衣服躺在鏡頭前颓影,是為了生存雌续;而你衣冠楚楚地站在鏡頭前,卻是為了私欲和欺騙熏迹!
5. 收集參數(shù)
def test(*params):
print('參數(shù)的長度是:',len(params))
print('第三個參數(shù)是:',params[2])
test(1,2,3,4,'Nigream',5)
參數(shù)的長度是: 6
第三個參數(shù)是: 3
6. 返回值
def back():
return [1,'Nigream',3.14]
print(back())
#運行結(jié)果
[1, 'Nigream', 3.14]
def back1():
return 1,'Nigream',3.14
#這里被看成一個元組
print(back1())
[1, 'Nigream', 3.14]
(1, 'Nigream', 3.14)
7. 作用域
def discounts(price,rate):
final_price = price * rate
return final_price
old_price = float(input('請輸入原價:'))
rate = float(input('請輸入折扣率:'))
new_price = discounts(old_price,rate)
print('打折后的價格是:',new_price)
請輸入原價:100
請輸入折扣率:0.8
打折后的價格是: 80.0
def discounts(price,rate):
final_price = price * rate
return final_price
old_price = float(input('請輸入原價:'))
rate = float(input('請輸入折扣率:'))
new_price = discounts(old_price,rate)
print('打折后的價格是:',new_price)
print('這里試圖打印局部變量final_price的值:',final_price)
請輸入原價:100
請輸入折扣率:0.8
打折后的價格是: 80.0
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-9-bd414db96855> in <module>()
7 new_price = discounts(old_price,rate)
8 print('打折后的價格是:',new_price)
----> 9 print('這里試圖打印局部變量final_price的值:',final_price)
NameError: name 'final_price' is not defined
def discounts(price,rate):
final_price = price * rate
print('這里試圖打印全局變量old_price的值:',old_price)
return final_price
old_price = float(input('請輸入原價:'))
rate = float(input('請輸入折扣率:'))
new_price = discounts(old_price,rate)
print('打折后的價格是:',new_price)
請輸入原價:100
請輸入折扣率:0.8
這里試圖打印局部變量old_price的值: 100.0
打折后的價格是: 80.0
def discounts(price,rate):
final_price = price * rate
#在這里python會重新定義一個名字相同的局部變量
old_price = 50
print('這里試圖打印局部變量old_price的1值:',old_price)
return final_price
old_price = float(input('請輸入原價:'))
rate = float(input('請輸入折扣率:'))
new_price = discounts(old_price,rate)
print('這里試圖打印全局變量old_price的2值:',old_price)
print('打折后的價格是:',new_price)
請輸入原價:100
請輸入折扣率:0.8
這里試圖打印局部變量old_price的1值: 50
這里試圖打印全局變量old_price的2值: 100.0
打折后的價格是: 80.0
8. global
count = 5
def MyFun():
count = 10
print(10)
MyFun()
print(count)
10
5
#要在函數(shù)內(nèi)部修改count
count = 5
def MyFun():
global count
count = 10
print(10)
MyFun()
print(count)
10
10
9. 內(nèi)嵌函數(shù)
def fun1():
print('fun1正在被調(diào)用')
def fun2():
print('fun2正在被調(diào)用')
fun2()
fun1()
fun1正在被調(diào)用
fun2正在被調(diào)用
10. 閉包
#如果在一個內(nèi)部函數(shù)里辑甜,對在外部作用域(但不是在全局作用域)的變量
#進行引用衰絮,那么內(nèi)部函數(shù)就被認為是閉包(closure)。
def FunX(x):
def FunY(y):
return x*y
return FunY
i = FunX(8)
print(type(i))
print(i(5))
print(FunX(8)(5))
<class 'function'>
40
40
def Fun1():
x = 5
def Fun2():
#這里由于外層作用域已經(jīng)定義了x磷醋,
#所以此時系統(tǒng)會重新定義局部變量x猫牡,
#而又未次局部變量賦值,所以該x = x + 2會報錯
x *= x
return x
return Fun2()
Fun1()
---------------------------------------------------------------------------
UnboundLocalError Traceback (most recent call last)
<ipython-input-32-d753abbbddb3> in <module>()
9 return Fun2()
10
---> 11 Fun1()
<ipython-input-32-d753abbbddb3> in Fun1()
7 x *= x
8 return x
----> 9 return Fun2()
10
11 Fun1()
<ipython-input-32-d753abbbddb3> in Fun2()
5 #所以此時系統(tǒng)會重新定義局部變量x邓线,
6 #而又未次局部變量賦值淌友,所以該x = x + 2會報錯
----> 7 x *= x
8 return x
9 return Fun2()
UnboundLocalError: local variable 'x' referenced before assignment
#python2解決,利用列表等容器
def Fun1():
#將其定義為列表
x = [5]
def Fun2():
x[0] *= x[0]
return x[0]
return Fun2()
Fun1()
25
#python3解決骇陈,利用關(guān)鍵字nonlocal
def Fun1():
x = 5
def Fun2():
nonlocal x
x *= x
return x
return Fun2()
Fun1()
25