Py学习  »  Python

再以这样的方式编写 Python 函数,你就OUT了!

IT服务圈儿 • 3 月前 • 326 次点击  
来源丨经授权转自 数据STUDIO

作者丨云朵君

Python就像一把神奇的瑞士军刀——语法简洁、表达力强、学习门槛低,新手也能快速上手。但说句掏心窝的话,即便是写过几年Python的老司机,在定义函数时也经常会掉进一些隐藏的坑里。

这些坑往往不会让程序立刻崩溃,表面上代码跑得挺顺畅。但时间一长,问题就暴露出来了:代码变得像一团乱麻,修个bug要花半天时间;性能慢慢变差,处理数据越来越慢;新同事接手时看得一头雾水,改都不敢改...

如果你写Python函数时还停留在"能跑就行"的阶段,那现在是时候提升代码质量了。今天我们就来好好聊聊函数编写中最容易踩的7个坑,以及如何用专业开发者的最佳实践来避开它们,让你的代码既优雅又高效。从参数设计到异常处理,从性能优化到可读性提升,这些技巧会让你写出更专业、更可靠的Python代码。

1. 停止使用默认可变参数!

错误的方法:

Python 只在函数定义时初始化默认参数一次,而不是每次调用函数时都初始化。这意味着同一个列表会在多次调用中共享,从而导致意想不到的行为。

def add_item(item, items=[]):
    items.append(item)
    return items

print(add_item('apple'))  # ['apple']
print(add_item('banana'))  # ['apple', 'banana'] ???

正确的方法:

对于可变参数,始终使用 None 作为默认值,并在函数内部进行初始化。

def add_item(item, items=None):
    if items is None:
        items = []
    items.append(item)
    return items

print(add_item('apple'))  # ['apple']
print(add_item('banana')) # ['banana']

2. 不要返回不同的数据类型

错误的方法:

返回不一致的数据类型会增加函数的工作难度,并可能导致意想不到的错误。

def process(value):
    if value > 10:
        return "Success"
     else:
        return 0  # Mixing str and int

正确的方法:

使用 None(或一致的类型)可使函数具有可预测性,更易于调试。

from typing import Optional

def process(value: int) -> Optional[str]:
    return "Success" if value > 10 else  None

3. 停止编写臃肿的函数--保持小巧

错误的方法:

这个函数做了太多事情。它计算价格、征税、考虑折扣并包含运费。

def calculate_price(quantity, price, tax_rate, discount, shipping):
    total = (quantity * price) + shipping
    total += total * tax_rate
    if discount:
        total -= total * discount
    return total

正确的方法(分解!):

现在,每个函数都能做好件事--更易于测试、调试和重用。

def calculate_subtotal(quantity, price):
    return quantity * price

def apply_tax(subtotal, tax_rate):
    return subtotal + (subtotal * tax_rate)

def apply_discount(amount, discount):
    return amount - (amount * discount)

def calculate_total(quantity, price, tax_rate, discount, shipping):
    subtotal = calculate_subtotal(quantity, price)
    taxed_total = apply_tax(subtotal, tax_rate)
    discounted_total = apply_discount(taxed_total, discount)
     return discounted_total + shipping

4. 使用 f 字符串而非老式字符串格式化

错误的方法:

def greet(name, age):
    return "Hello, my name is %s and I am %d years old." % (name, age)

或者

def greet(name, age):
    return  "Hello, my name is {} and I am {} years old.".format(name, age)

正确的方法(更易读、更高效):

def greet(name, age):
    return f"Hello, my name is {name} and I am {age} years old."

f-strings 更快、更易读,是在 Python 中格式化字符串的首选方式。

5. 利用类型提示提高清晰度

错误的方法:

这种方法可行,但  a 和 b 是什么类型?整数?浮点数?字符串?

def add_numbers(a, b):
    return a + b

正确的方法:

类型提示使函数自文档化并防止意外行为。

def add_numbers(a: int, b: int) -> int:
    return a + b

6. 使用 enumerate() 代替手动跟踪索引

错误的方法:

fruits = ["apple""banana""cherry"]
index = 0
for fruit in fruits:
    print(f"{index}: {fruit}" )
    index += 1

正确的方法:

enumerate()消除了手动索引跟踪,使循环更简洁、更 Pythonic。

fruits = ["apple""banana""cherry"]
for index, fruit in enumerate(fruits):
    print(f"{index}: {fruit}")

7. 避免使用流量控制的 try-except

错误的方法:

def get_price(data):
    try:
        return data["price"]
    except KeyError:
        return 0

正确的方法:

使用 .get()更易读,并可避免不必要的异常处理。

def get_price(data):
    return data.get("price", 0)

写在最后

编写更好的 Python 函数不仅仅是为了让它们工作,更是为了让它们可读、可维护和高效。通过避免这些常见错误并遵循最佳实践,你将写出更简洁、更专业的 Python 代码。

现在你知道如何改进 Python 函数了,回到你的旧代码并重构它!你会惊喜地发现代码变得更简洁、更高效了。

还有其他函数编写技巧吗?欢迎在下面的评论中留言!

1、糟糕,线上库存竟然变成负500。。。
2、Vue 团队又一重磅力作来了!
3、没想到,更改import方式,Python 启动提速 5 倍!
4、操作系统是如何一步步发明系统调用机制的?
5、继续卷,Google 发布AI 编程工具 Firebase Studio

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/181273
 
326 次点击