一鍵掌握:Python 函數(shù)聲明與調(diào)用的 20 個優(yōu)秀實踐
今天我們要一起探索的是Python世界中的一塊基石——函數(shù)!想象一下,像魔術(shù)師一樣,輕輕一揮手,復(fù)雜的任務(wù)就被封裝成簡潔的命令,這就是函數(shù)的魅力。下面,讓我們用最接地氣的方式,揭開它的神秘面紗,掌握那些讓代碼飛起來的20個小技巧。
1. 基礎(chǔ)中的基礎(chǔ):Hello, Function!
def say_hello(name="World"):
print(f"Hello, {name}!")
say_hello("Pythonista") # 輸出: Hello, Pythonista!
解密:def是定義函數(shù)的關(guān)鍵詞,say_hello是函數(shù)名,括號內(nèi)是參數(shù),如果沒有提供參數(shù),就用默認值。
2. 參數(shù)傳遞:位置VS關(guān)鍵字
def greet(firstName, lastName):
print(f"Hi, I'm {firstName} {lastName}")
greet(lastName="Smith", firstName="John") # 明確指定參數(shù)名
小貼士:通過名字指定參數(shù),讓代碼更易讀,特別是參數(shù)多時。
3. *args 和 **kwargs:無限參數(shù)的秘密
def super_greet(*names): # *args 收集位置參數(shù)
for name in names:
print(f"Hello, {name}!")
super_greet("Alice", "Bob", "Charlie") # 多個名字一次性處理
def versatile_greet(**details): # **kwargs 收集關(guān)鍵字參數(shù)
for key, value in details.items():
print(f"{key.title()}: {value}")
versatile_greet(age=25, city="New York") # 關(guān)鍵信息一網(wǎng)打盡
神奇之處:*args和**kwargs讓你的函數(shù)可以接受任意數(shù)量的參數(shù),超級靈活!
4. 返回值不只是一個
def multiple_returns():
return "Success", 200
result, status = multiple_returns()
print(result, status) # 輸出: Success 200
多才多藝:函數(shù)可以返回多個值,其實是以元組的形式返回的。
5. 文檔字符串:讓代碼會說話
def calculate_area(radius):
"""
計算圓的面積。
參數(shù):
radius (float): 圓的半徑
返回:
float: 圓的面積
"""
import math
return math.pi * radius**2
print(calculate_area.__doc__) # 查看文檔字符串
文明交流:良好的文檔字符串是團隊合作的潤滑劑,也是自我復(fù)習的好幫手。
6. 默認參數(shù)的坑
def append_to_list(item, my_list=[]):
my_list.append(item)
return my_list
print(append_to_list(1)) # [1]
print(append_to_list(2)) # 注意!這里會是 [1, 2],不是預(yù)期的 [2]
警告:默認參數(shù)在函數(shù)定義時就初始化了,多次調(diào)用時會保留之前的值,小心這個陷阱。
7. 變量作用域:誰能訪問我?
x = "global"
def scope_test():
x = "local"
print(x) # local
scope_test()
print(x) # global
名字游戲:在函數(shù)內(nèi)部定義的變量默認是局部的,不會影響到外部的同名變量。
8. 非局部變量的修改
y = 10
def modify_outer():
global y # 告訴Python你想修改外部的y
y = 20
modify_outer()
print(y) # 輸出: 20
特權(quán)操作:使用global關(guān)鍵字可以讓函數(shù)內(nèi)部修改全局變量,但要謹慎使用。
9. 閉包:函數(shù)內(nèi)的函數(shù)
def counter():
count = 0
def increment():
nonlocal count
count += 1
return count
return increment
my_counter = counter()
print(my_counter()) # 1
print(my_counter()) # 2
內(nèi)外有別:閉包允許內(nèi)部函數(shù)訪問并修改外部函數(shù)的變量,而外部函數(shù)返回的是內(nèi)部函數(shù)的引用。
10. 裝飾器:給函數(shù)穿上花衣
def my_decorator(func):
def wrapper():
print("Something is happening before the function is called.")
func()
print("Something is happening after the function is called.")
return wrapper
@my_decorator
def say_hello():
print("Hello!")
say_hello()
裝飾生活,裝飾函數(shù):裝飾器是Python的一大特色,它可以在不修改原函數(shù)代碼的情況下增加新功能。
高級使用場景
11. 遞歸:自己調(diào)用自己的藝術(shù)
def factorial(n):
if n == 1:
return 1
else:
return n * factorial(n-1)
print(factorial(5)) # 輸出: 120
無限循環(huán)的智慧:遞歸是解決某些問題的強大工具,但要注意避免無限循環(huán),確保有一個清晰的終止條件。
12. 匿名函數(shù)lambda:簡潔之美
double = lambda x: x * 2
print(double(5)) # 輸出: 10
squared = lambda x: x**2
numbers = [1, 2, 3]
print(list(map(squared, numbers))) # 輸出: [1, 4, 9]
一閃即逝的美:lambda函數(shù)適合簡單的操作,它們無需定義即可使用,非常適合用在高階函數(shù)中。
13. map()函數(shù):批量操作的藝術(shù)
def square(n):
return n*n
numbers = [1, 2, 3, 4]
squared_numbers = list(map(square, numbers))
print(squared_numbers) # 輸出: [1, 4, 9, 16]
# 或者用lambda簡化
simplified = list(map(lambda x: x*x, numbers))
print(simplified) # 同上
批量處理好幫手:map函數(shù)對序列的每個元素應(yīng)用指定函數(shù),返回一個迭代器對象,通常轉(zhuǎn)換為列表使用。
14. filter()函數(shù):篩選高手
def is_even(n):
return n % 2 == 0
numbers = [1, 2, 3, 4, 5, 6]
even_numbers = list(filter(is_even, numbers))
print(even_numbers) # 輸出: [2, 4, 6]
# 簡化版
even_with_lambda = list(filter(lambda x: x % 2 == 0, numbers))
print(even_with_lambda) # 同上
只選對的:filter函數(shù)根據(jù)提供的函數(shù)來篩選序列中的元素,返回一個迭代器,同樣常用list轉(zhuǎn)換。
15. reduce()函數(shù):累積計算的秘密武器
from functools import reduce
def accumulator(acc, item):
return acc + item
numbers = [1, 2, 3, 4]
sum_of_numbers = reduce(accumulator, numbers, 0)
print(sum_of_numbers) # 輸出: 10
# 或用lambda簡化
sum_with_lambda = reduce(lambda acc, item: acc + item, numbers, 0)
print(sum_with_lambda) # 同上
累積力量:reduce將一個函數(shù)應(yīng)用于序列的所有元素,累積結(jié)果,非常適合求和、乘積等操作。
16. 偏函數(shù)partial:定制化的便捷
from functools import partial
def power(base, exponent):
return base ** exponent
square = partial(power, exponent=2)
print(square(5)) # 輸出: 25
cube = partial(power, exponent=3)
print(cube(3)) # 輸出: 27
定制你的函數(shù):偏函數(shù)可以固定原函數(shù)的部分參數(shù),生成新的函數(shù),非常適用于需要多次調(diào)用且參數(shù)變化不大的場景。
17. 遞歸優(yōu)化與尾遞歸
# 注意:Python標準解釋器不直接支持尾遞歸優(yōu)化
def factorial_tail(n, accumulator=1):
if n == 1:
return accumulator
else:
return factorial_tail(n-1, n*accumulator)
print(factorial_tail(5)) # 輸出: 120
尾聲:雖然Python沒有內(nèi)置的尾遞歸優(yōu)化,理解尾遞歸的概念對理解函數(shù)調(diào)用棧很有幫助。
18. 閉包進階:數(shù)據(jù)封裝
def counter_maker():
count = 0
def increment():
nonlocal count
count += 1
return count
return increment
counter1 = counter_maker()
counter2 = counter_maker()
print(counter1(), counter1()) # 輸出: 1 2
print(counter2(), counter2()) # 輸出: 1 2
工廠模式:閉包可以用來創(chuàng)建具有獨立狀態(tài)的函數(shù),類似于面向?qū)ο笾械膶嵗?/p>
19. 高階函數(shù):函數(shù)的函數(shù)
def apply_operation(func, a, b):
return func(a, b)
add = lambda x, y: x + y
subtract = lambda x, y: x - y
print(apply_operation(add, 5, 3)) # 輸出: 8
print(apply_operation(subtract, 5, 3)) # 輸出: 2
函數(shù)的魔力:高階函數(shù)可以接受函數(shù)作為參數(shù)或返回函數(shù),這是函數(shù)式編程的核心概念。
20. 裝飾器進階:帶參數(shù)的裝飾器
def repeat(n):
def decorator(func):
def wrapper(*args, **kwargs):
for _ in range(n):
func(*args, **kwargs)
return wrapper
return decorator
@repeat(3)
def say_hello():
print("Hello!")
say_hello() # 輸出: Hello! Hello! Hello!
裝飾器的新維度:帶參數(shù)的裝飾器讓裝飾器本身也變得靈活,可以根據(jù)需要調(diào)整行為。
至此,我們探索了Python函數(shù)從基礎(chǔ)到進階的20個最佳實踐,每一個點都是打開新視野的鑰匙。