Python中5种常见的反模式

开发 后端
Python 是2021年最流行的编程语言之一。简单的语法使它成为很多程序员的入门首选。由于 Python 具有动态性和灵活性,有时候开发人员很容易编写出错误及低效的代码。

 本文将向大家介绍 Python 中常见的反模式,并给出了更好的编译方法。

[[394520]]

1.对Iterable对象使用map()和filter()

内置的 map 和 filter 可以帮助我们通过函数编程的原理在 Python 中转换 iterable 对象。

这两个方法都接受一个函数和一个 iterable 作为参数,并返回相应的对象。

通过将该对象作为参数传递到 Python 中的内置列表构造函数,可以将其转换为列表。

我们经常使用 lambda 函数作为 map、filter 函数的参数:

  1. my_list = [1, 2, 3, 4 ,5, 6, 7, 8, 9, 10] 
  2.  
  3. # 将每个元素乘以2 
  4. print(list(map(lambda x: x * 2, my_list)))  
  5. # [2, 4, 6, 8, 10, 12, 14, 16, 18, 20] 
  6.  
  7. # 过滤掉偶数 
  8. print(list(filter(lambda x: x % 2 == 0, my_list)))  
  9. # [2, 4, 6, 8, 10] 

上面的代码看起来相当累赘和不清楚。使用列表理解可以实现相同结果:

  1. my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 
  2.  
  3. # 与map相同 
  4. print([x * 2 for x in my_list]) 
  5. # [2, 4, 6, 8, 10, 12, 14, 16, 18, 20] 
  6.  
  7. # 与filter相同 
  8. print([x for x in my_list if x % 2 == 0]) 
  9. # [2, 4, 6, 8, 10] 

不使用lambda函数后,列表理解变得更具可读性和简洁性。

2.输入较大时使用列表理解

列表理解有助于我们编写出清晰、简洁的代码。

但是,列表理解总是为 iterable 中的每个值创建一个列表。当输入量非常大时,就会导致内存占用过大的问题:我们的机器可能会崩溃。

生成器表达式结合了列表理解和生成器这两个方面的优点,为处理大型输入序列提供了更有效的方法。

要创建生成器表达式,只需将列表中的 [] 方括号替换为()方括号。


生成器表达式并不是创建一个全新的列表,而是创建一个迭代器。

这会降低创建速度并优化内存分配。我们可以使用 next 函数或通过循环访问生成器表达式的每个后续元素。

  1. my_list = [1, 2, 3, 4 ,5, 6, 7, 8, 9, 10] 
  2.  
  3. my_gen_expr = (x * 2 for x in my_list) 
  4.  
  5. print(next(my_gen_expr)) 
  6. print(next(my_gen_expr)) 
  7. # >> 
  8. # 2 
  9. # 4 
  10.  
  11. for x in my_gen_expr: 
  12.   print(x) 
  13. # >> 
  14. # 6 
  15. # 8 
  16. # 10 
  17. # 12 
  18. # 14 
  19. # 16 
  20. # 18 
  21. # 20 

注:生成器表达式是有状态的,因此在重用时要注意。如果要多次使用迭代器,则可能需要重新创建迭代器。

3.不使用range()的情况

range 函数对迭代整数很有用。

  1. for i in range(10): 
  2.     print(i) 

当迭代类似列表的数据结构时,我们可以完全依赖for循环语法来访问每个项目。代码如下:

  1. my_list = [2, 4, 6, 8, 10] 
  2.  
  3. for item in my_list: 
  4.     print(item) 
  5.  
  6. # Output: 
  7. # 2 
  8. # 4 
  9. # 6 
  10. # 8 
  11. # 10 

但是,当想要访问索引和元素时,我们可以使用列表长度下的 range 方法,如下所示:

  1. my_list = [2, 4, 6, 8, 10] 
  2.  
  3. for i in range(len(my_list)): 
  4.     print("index: ", i, "value: ", my_list[i]) 
  5.      
  6. Output
  7. index: 0 value: 2 
  8. index: 1 value: 4 
  9. index: 2 value: 6 
  10. index: 3 value: 8 
  11. index: 4 value: 10 

代码看起来不可读,因为我们必须在列表上调用 len,然后使用 range 方法包装输出。为了使代码更加具有 python 风格,我们必须提高代码的可读性。

更好的方法是对 list 对象调用 enumerate 函数。这将创建一个生成器,生成列表项的索引和值。

  1. my_list = [2, 4, 6, 8, 10] 
  2.  
  3. for i, v in enumerate(my_list): 
  4.     print("index: ", i, "value: ", v) 
  5.      
  6. Output
  7. index: 0 value: 2 
  8. index: 1 value: 4 
  9. index: 2 value: 6 
  10. index: 3 value: 8 
  11. index: 4 value: 10 

代码看起来是不是更加干净了?

4.字典中键丢失的问题

字典具有快速访问、分配、插入和删除的能力,是一种非常流行的数据结构。

但是新手开发人员访问字典中不存在的密钥时经常会遇到问题。

  1. crypto_price = { 
  2.   "Bitcoin": 64000, 
  3.   "Ethereum": 2300, 
  4.   "Dogecoin": 0.12 
  5.  
  6. crypto_price["XRP"

处理该类问题的其中一种方法是检查字典中是否存在密钥,代码如下:

  1. key = "XRP" 
  2.  
  3. if key not in crypto_price: 
  4.     crypto_price[key] = 1.2 
  5.      
  6. print(crypto_price[key]) 

另一种方法是使用 try/except 块,如下所示:

  1. key = "XRP" 
  2.  
  3. try: 
  4.     xrp = crypto_price[key
  5. except raise KeyError: 
  6.     xrp = 1.2 
  7.      
  8. crypto_price[key] = xrp 

上面的代码确实实现了我们的目标,但是我们可以通过使用字典方法 get 进一步改进。

通过使用 get 方法来获取相应键的值,而不是使用方括号 [] 来访问字典的键。

另外,如果键不存在,get 方法将返回 None,而不是抛出 KeyError。如果缺少键而不是无键,还可以将参数传递给 get 方法以获取默认值。

  1. key = "XRP" 
  2.  
  3. if crypto_price.get("XRP"is None: 
  4.   crypto_price["XRP"] = 1.2 
  1. ada = crypto_price.get("ADA", 0) 
  2.  
  3. # Prints 0 
  4. print(ada) 

5.惰性关键字和位置参数设计

Python函数能够同时接受位置参数和关键字参数。

位置参数是不后跟等号(=)和默认值的名称。

关键字参数后面跟一个等号和一个给出其默认值的表达式。

得益于这种设计,python函数的创建和重用非常灵活。

但是,定义函数时,错误的设计选择可能会导致代码中难以修复的错误。

我们以计算复利的函数为例:

  1. # 复利计算器年/月复利 
  2. def calculate_compound_interest(principal, rate, time_in_years, 
  3.                                 compounded_monthly, to_string): 
  4.   t = 1 
  5.   if compounded_monthly: 
  6.     t = 12 
  7.   amt = principal * (1 + rate/(t * 100)) ** (time_in_years * t) 
  8.   if to_string: 
  9.     return f"${amt - principal:.2f}" 
  10.  
  11.   return amt - principal 
  12.     
  13.    
  14.   calculate_compound_interest(100, 5, 2, FalseFalse
  15.   # 10.25 

调用函数时出现的一个问题是,两个布尔参数(compounded_monthly 和结尾的 to_string)很容易相互混淆。这就会出现难以追踪的问题。

我们可以通过如下方式更改函数定义来提高可读性:

  1. # 复利计算器年/月复利 
  2. def calculate_compound_interest(principal, rate, time_in_years, 
  3.                                 compounded_monthly=False, to_string=False): 

通过将两个布尔参数指定为关键字参数,函数调用方可以显式地指定要设置的布尔值,这些值将覆盖默认值。

  1. calculate_compound_interest(100, 5, 2, compounded_monthly=True
  2. # 10.49413355583269 
  3.  
  4. calculate_compound_interest(100, 5, 2, to_string=True
  5. '$10.25' 

但是,这仍然会出现问题,主要原因是关键字参数是可选的,因为没有任何强制调用方将这些作为关键字参数使用。

因此,我们仍然可以使用旧方法调用该函数:

  1. calculate_compound_interest(100, 5, 2, FalseFalse

解决该问题的方法是仅在定义函数时强制布尔参数为关键字:

  1. # 复利计算器年/月复利 
  2. def calculate_compound_interest(principal, rate, time_in_years, *, # Changed 
  3.                                 compounded_monthly=False, to_string=False): 

我们看到,*符号表示位置参数的结束和仅关键字参数的开始。

如果这样调用:

  1. calculate_compound_interest(100, 5, 2, FalseFalse

将发生以下错误:

  1. --------------------------------------------------------------------------- 
  2. TypeError                                 Traceback (most recent call last
  3. <ipython-input-32-faf75d2ad121> in <module> 
  4. ----> 1 print(calculate_compound_interest(1000, 5, 2, False, False)) 
  5. TypeError: calculate_compound_interest() takes 3 positional arguments but 5 were given 

但是,关键字参数及其默认行为仍将保持不变,如下所示:

  1. alculate_compound_interest(100, 5, 2, compounded_monthly=True
  2. # 10.49413355583269 
  3.  
  4. calculate_compound_interest(100, 5, 2, to_string=True
  5. '$10.25' 

然而,仍然存在一个问题。

假设调用者决定对前三个必需参数(principal、rate、time in years)混合使用位置和关键字。

如果这三个参数的函数参数名称发生更改,我们将看到Python解释器。它会这样说:

  1. # 复利计算器年/月复利 
  2. def calculate_compound_interest(p, r, t_in_y, *, # Changed 
  3.                                 compounded_monthly=False, to_string=False): 
  1. calculate_compound_interest(principal=1000, rate=5, time_in_years=2) 
  2.  
  3. calculate_compound_interest(1000, 5, time_in_years=2) 

将发生以下错误:

  1. --------------------------------------------------------------------------- 
  2. TypeError                                 Traceback (most recent call last
  3. <ipython-input-36-42e7ec842cd5> in <module> 
  4. ----> 1 calculate_compound_interest(principal=1000, rate=5, time_in_years=2) 
  5. TypeError: calculate_compound_interest() got an unexpected keyword argument 'principal' 
  6. --------------------------------------------------------------------------- 
  7. TypeError                                 Traceback (most recent call last
  8. <ipython-input-37-1bc57c40980f> in <module> 
  9. ----> 1 calculate_compound_interest(1000, 5, time_in_years=2) 
  10. TypeError: calculate_compound_interest() got an unexpected keyword argument 'time_in_years' 

因为我们没有考虑调用方显式地使用位置参数,所以代码中断。

python3.8中引入了一个解决方案,我们可以使用/参数重新定义函数,该参数指示仅位置参数的结束位置。代码如下:

  1. # 复利计算器年/月复利 
  2. def calculate_compound_interest(p, r, t_in_y, /, *,  # 改变 
  3.                                 compounded_monthly=False, to_string=False): 

现在这样调用函数就会产生正确的结果:

  1. calculate_compound_interest(100, 5, 2, compounded_monthly=True
  2. # 10.49413355583269 
  3.  
  4. calculate_compound_interest(100, 5, 2, to_string=True
  5. '$10.25' 

但是,如果我们这样调用:

  1. calculate_compound_interest(p=1000, r=5, t_in_y=2) 

也会显示相应的错误:

  1. ---------------------------------------------------------------------------  
  2. TypeError                                 Traceback (most recent call last)  
  3. <ipython-input-21-883e876a7e8b> in <module>  
  4. ----> 1 calculate_compound_interest(p=1000, r=5, t_in_y=2)  
  5.       2  
  6. TypeError: calculate_compound_interest() got some positional-only arguments passed as keyword arguments: 'p, r, t_in_y'  

以上。

 

责任编辑:华轩 来源: 今日头条
相关推荐

2014-07-30 10:08:13

Python反模式

2009-06-29 18:11:40

JSP设计模式

2020-09-14 08:30:44

Kubernetes容器

2023-12-22 14:27:30

2020-10-09 06:52:31

设计模式软件

2010-09-06 09:26:07

PPP协议

2015-10-10 11:23:17

Java常量反模式

2015-09-22 10:56:13

Java反模式

2010-10-13 15:33:38

MySQL日志

2020-06-28 10:15:39

架构模式软件

2017-09-14 09:30:38

软件架构模式

2021-04-13 11:32:34

开源开源治理开源代码项目

2010-06-18 09:19:39

UML面向对象建模

2022-11-03 15:36:44

事件响应反模式系统

2011-05-24 09:30:26

Findbugs

2020-09-11 10:36:24

设计模式代码

2020-03-19 22:16:05

数据概率分布Python实现

2012-02-02 09:21:39

编程

2021-10-29 05:53:51

前端测试开发代码

2011-06-30 14:45:52

外链
点赞
收藏

51CTO技术栈公众号