每个开发人员仍然面临的 ython 错误以及如何修复它们)(开发人员.修复.面临.错误.ython...)

wufei1232024-09-08python2

每个开发人员仍然面临的 ython 错误以及如何修复它们)

由 rupesh sharma 又名 @hackyrupesh

撰写

python 以其简单和美观而成为世界上最流行的编程语言之一。然而,即使到了 2024 年,某些缺陷仍然困扰着开发者。这些问题并不总是由于 python 的弱点造成的,而是由于它的设计、行为或常见的误解导致了意外的结果。在这篇博客文章中,我们将了解每个开发人员在 2024 年仍然遇到的 5 大 python 问题及其补救措施。

1. 可变默认参数:无声陷阱 问题

最臭名昭著的 python 错误之一是可变的默认参数。当可变对象(如列表或字典)用作函数中的默认参数时,python 仅在定义函数时计算此默认参数一次,而不是每次调用函数时计算此默认参数。当函数修改对象时,这会导致意外的行为。

例子
def append_to_list(value, my_list=[]):
    my_list.append(value)
    return my_list

print(append_to_list(1))  # outputs: [1]
print(append_to_list(2))  # outputs: [1, 2] - unexpected!
print(append_to_list(3))  # outputs: [1, 2, 3] - even more unexpected!
解决方案

为了避免这种情况,请使用 none 作为默认参数,并根据需要在函数内创建一个新列表。

def append_to_list(value, my_list=none):
    if my_list is none:
        my_list = []
    my_list.append(value)
    return my_list

print(append_to_list(1))  # outputs: [1]
print(append_to_list(2))  # outputs: [2]
print(append_to_list(3))  # outputs: [3]
参考
  • python 的默认参数陷阱
2. 字典中难以捉摸的keyerror 问题

尝试访问不存在的字典键时会发生keyerror。当使用嵌套字典或处理结构无法保证的数据时,这可能特别棘手。

例子
data = {'name': 'alice'}
print(data['age'])  # raises keyerror: 'age'
解决方案

为了防止 keyerror,请使用 get() 方法,如果找不到密钥,该方法将返回 none (或指定的默认值)。

print(data.get('age'))  # outputs: none
print(data.get('age', 'unknown'))  # outputs: unknown

对于嵌套字典,请考虑使用集合模块或 dotmap 或 pydash 等库中的 defaultdict。

from collections import defaultdict

nested_data = defaultdict(lambda: 'unknown')
nested_data['name'] = 'alice'
print(nested_data['age'])  # outputs: unknown
参考
  • python keyerror 及其处理方法
3. try- except 过度使用导致的无提示错误 问题

过度使用或误用 try- except 块可能会导致静默错误,即捕获异常但未正确处理异常。这可能会使错误难以检测和调试。

例子
try:
    result = 1 / 0
except:
    pass  # silently ignores the error
print("continuing execution...")

在上面的示例中,zerodivisionerror 被捕获并忽略,但这可以掩盖潜在的问题。

解决方案

始终指定您要捕获的异常类型,并适当处理它。记录错误还可以帮助追踪问题。

try:
    result = 1 / 0
except zerodivisionerror as e:
    print(f"error: {e}")
print("continuing execution...")

对于更广泛的异常处理,您可以使用日志记录而不是传递:

import logging

try:
    result = 1 / 0
except exception as e:
    logging.error(f"unexpected error: {e}")
参考
  • python 的 try- except 最佳实践
4. 整数除法:截断陷阱 问题

在python 3之前,两个整数相除默认执行向下取整除法,将结果截断为整数。尽管 python 3 通过真正的除法 (/) 解决了这个问题,但一些开发人员在无意中使用楼层除法 (//) 时仍然面临问题。

例子
print(5 / 2)  # outputs: 2.5 in python 3, but would be 2 in python 2
print(5 // 2)  # outputs: 2
解决方案

除非您特别需要楼层划分,否则始终使用 / 进行划分。将代码从 python 2 移植到 python 3 时要小心。

print(5 / 2)  # outputs: 2.5
print(5 // 2)  # outputs: 2

为了获得清晰且可预测的代码,请考虑使用decimal.decimal进行更准确的算术运算,尤其是在金融计算中。

from decimal import decimal

print(decimal('5') / decimal('2'))  # outputs: 2.5
参考
  • python 划分:/ vs //
5. 循环引用导致内存泄漏 问题

python 的垃圾收集器处理大部分内存管理,但如果处理不当,循环引用可能会导致内存泄漏。当两个或多个对象相互引用时,它们可能永远不会被垃圾回收,从而导致内存使用量增加。

例子
class node:
    def __init__(self, value):
        self.value = value
        self.next = none

node1 = node(1)
node2 = node(2)
node1.next = node2
node2.next = node1  # circular reference

del node1
del node2  # memory not freed due to circular reference
解决方案

为了避免循环引用,请考虑通过weakref模块使用弱引用,该模块允许在不存在强引用时对引用进行垃圾收集。

import weakref

class node:
    def __init__(self, value):
        self.value = value
        self.next = none

node1 = node(1)
node2 = node(2)
node1.next = weakref.ref(node2)
node2.next = weakref.ref(node1)  # no circular reference now

或者,您可以在删除对象之前通过将引用设置为 none 来手动打破循环。

node1.next = None
node2.next = None
del node1
del node2  # Memory is freed
参考
  • python 内存管理和垃圾收集
结论

即使到了 2024 年,python 开发人员仍然会遇到这些常见的错误。尽管多年来该语言不断发展和改进,但这些问题通常与 python 工作原理的基本方面有关。通过了解这些陷阱并应用适当的解决方案,您可以编写更健壮、无错误的代码。快乐编码!

作者:rupesh sharma,又名@hackyrupesh

以上就是每个开发人员仍然面临的 ython 错误以及如何修复它们)的详细内容,更多请关注知识资源分享宝库其它相关文章!

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。