今天我們要一起探索的是Python世界中的一塊基石——函數!想象一下,像魔術師一樣,輕輕一揮手,復雜的任務就被封裝成簡潔的命令,這就是函數的魅力。下面,讓我們用最接地氣的方式,揭開它的神秘面紗,掌握那些讓代碼飛起來的20個小技巧。
def say_hello(name="World"): print(f"Hello, {name}!")say_hello("Pythonista") # 輸出: Hello, Pythonista!
解密:def是定義函數的關鍵詞,say_hello是函數名,括號內是參數,如果沒有提供參數,就用默認值。
def greet(firstName, lastName): print(f"Hi, I'm {firstName} {lastName}")greet(lastName="Smith", firstName="John") # 明確指定參數名
小貼士:通過名字指定參數,讓代碼更易讀,特別是參數多時。
def super_greet(*names): # *args 收集位置參數 for name in names: print(f"Hello, {name}!") super_greet("Alice", "Bob", "Charlie") # 多個名字一次性處理def versatile_greet(**details): # **kwargs 收集關鍵字參數 for key, value in details.items(): print(f"{key.title()}: {value}")versatile_greet(age=25, city="New York") # 關鍵信息一網打盡
神奇之處:*args和**kwargs讓你的函數可以接受任意數量的參數,超級靈活!
def multiple_returns(): return "Success", 200result, status = multiple_returns()print(result, status) # 輸出: Success 200
多才多藝:函數可以返回多個值,其實是以元組的形式返回的。
def calculate_area(radius): """ 計算圓的面積。 參數: radius (float): 圓的半徑 返回: float: 圓的面積 """ import math return math.pi * radius**2print(calculate_area.__doc__) # 查看文檔字符串
文明交流:良好的文檔字符串是團隊合作的潤滑劑,也是自我復習的好幫手。
def append_to_list(item, my_list=[]): my_list.append(item) return my_listprint(append_to_list(1)) # [1]print(append_to_list(2)) # 注意!這里會是 [1, 2],不是預期的 [2]
警告:默認參數在函數定義時就初始化了,多次調用時會保留之前的值,小心這個陷阱。
x = "global"def scope_test(): x = "local" print(x) # localscope_test()print(x) # global
名字游戲:在函數內部定義的變量默認是局部的,不會影響到外部的同名變量。
y = 10def modify_outer(): global y # 告訴Python你想修改外部的y y = 20modify_outer()print(y) # 輸出: 20
特權操作:使用global關鍵字可以讓函數內部修改全局變量,但要謹慎使用。
def counter(): count = 0 def increment(): nonlocal count count += 1 return count return incrementmy_counter = counter()print(my_counter()) # 1print(my_counter()) # 2
內外有別:閉包允許內部函數訪問并修改外部函數的變量,而外部函數返回的是內部函數的引用。
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_decoratordef say_hello(): print("Hello!")say_hello()
裝飾生活,裝飾函數:裝飾器是Python的一大特色,它可以在不修改原函數代碼的情況下增加新功能。
def factorial(n): if n == 1: return 1 else: return n * factorial(n-1)print(factorial(5)) # 輸出: 120
無限循環的智慧:遞歸是解決某些問題的強大工具,但要注意避免無限循環,確保有一個清晰的終止條件。
double = lambda x: x * 2print(double(5)) # 輸出: 10squared = lambda x: x**2numbers = [1, 2, 3]print(list(map(squared, numbers))) # 輸出: [1, 4, 9]
一閃即逝的美:lambda函數適合簡單的操作,它們無需定義即可使用,非常適合用在高階函數中。
def square(n): return n*nnumbers = [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函數對序列的每個元素應用指定函數,返回一個迭代器對象,通常轉換為列表使用。
def is_even(n): return n % 2 == 0numbers = [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函數根據提供的函數來篩選序列中的元素,返回一個迭代器,同樣常用list轉換。
from functools import reducedef accumulator(acc, item): return acc + itemnumbers = [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將一個函數應用于序列的所有元素,累積結果,非常適合求和、乘積等操作。
from functools import partialdef power(base, exponent): return base ** exponentsquare = partial(power, exponent=2)print(square(5)) # 輸出: 25cube = partial(power, exponent=3)print(cube(3)) # 輸出: 27
定制你的函數:偏函數可以固定原函數的部分參數,生成新的函數,非常適用于需要多次調用且參數變化不大的場景。
# 注意:Python標準解釋器不直接支持尾遞歸優化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沒有內置的尾遞歸優化,理解尾遞歸的概念對理解函數調用棧很有幫助。
def counter_maker(): count = 0 def increment(): nonlocal count count += 1 return count return incrementcounter1 = counter_maker()counter2 = counter_maker()print(counter1(), counter1()) # 輸出: 1 2print(counter2(), counter2()) # 輸出: 1 2
工廠模式:閉包可以用來創建具有獨立狀態的函數,類似于面向對象中的實例。
def apply_operation(func, a, b): return func(a, b)add = lambda x, y: x + ysubtract = lambda x, y: x - yprint(apply_operation(add, 5, 3)) # 輸出: 8print(apply_operation(subtract, 5, 3)) # 輸出: 2
函數的魔力:高階函數可以接受函數作為參數或返回函數,這是函數式編程的核心概念。
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!
裝飾器的新維度:帶參數的裝飾器讓裝飾器本身也變得靈活,可以根據需要調整行為。
至此,我們探索了Python函數從基礎到進階的20個最佳實踐,每一個點都是打開新視野的鑰匙。
本文鏈接:http://www.tebozhan.com/showinfo-26-96421-0.html一鍵掌握:Python 函數聲明與調用的 20 個優秀實踐
聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。郵件:2376512515@qq.com