Home  >  Article  >  Backend Development  >  Antipatterns in Python Programming

Antipatterns in Python Programming

黄舟
黄舟Original
2017-02-04 16:53:511335browse

This article collects irregular but occasionally subtle problems that I have seen in code written by novice Python developers. The purpose of this article is to help novice developers get past the stage of writing ugly Python code. In order to take care of the target audience, this article has made some simplifications (for example, when discussing iterators, generators and the powerful iteration tool itertools are ignored).

For those newbie developers out there, there are always some reasons to use anti-patterns, and I've tried to give them where possible. But usually these anti-patterns result in code that is less readable, more prone to bugs, and does not conform to Python's coding style. If you are looking for more relevant introduction materials, I highly recommend The Python Tutorial or Dive into Python.

Iteration

Use of range

Newbies to Python programming like to use range to implement simple iteration, and obtain iterations within the length range of the iterator Each element in the container:

for i in range(len(alist)):
    print alist[i]


It should be remembered that range is not intended to implement simple iteration of the sequence. Compared with those for loops defined with numbers, although the for loop implemented with range seems natural, it is prone to bugs when used in sequence iteration, and it is not as clear as directly constructing an iterator:

for item in alist:
    print item


Abuse of range can easily lead to unexpected off-by-one errors. This is usually caused by novice programmers forgetting that the object generated by range includes the first parameter of the range but not the first parameter of the range. The second one is similar to substring and many other functions of this type in Java. New programmers who think that the end of the sequence is not exceeded will create bugs:

# 
迭代整个序列错误的方法
alist = ['her', 'name', 'is', 'rio']
for i in range(0, len(alist) - 1): # 大小差一(Off by one)!
    print i, alist[i]

Common reasons for using ranges inappropriately:


1. Use indexes in loops. This is not a valid reason, you can use the following instead of using the index:

for index, value in enumerate(alist):
    print index, value

2. You need to iterate two loops at the same time and use the same index to get two values. In this case, zip can be used:

for word, number in zip(words, numbers):
    print word, number

3. Part of the sequence needs to be iterated. In this case, it can be achieved by just iterating over the sequence slice. Be sure to add the necessary comments to indicate the purpose:

for word in words[1:]: # 不包括第一个元素
    print word

There is an exception: when you iterate over a large sequence, the overhead caused by the slicing operation It's relatively big. If the sequence only has 10 elements, this is no problem; but if there are 10 million elements, or if the slicing operation is performed in a performance-sensitive inner loop, the overhead becomes very important. In this case, you can consider using xrange instead of range [1].


Besides being used to iterate over a sequence, an important use of range is when you actually want to generate a sequence of numbers rather than to generate an index:

# 
Print foo(x) for 0<=x<5
for x in range(5):
    print foo(x)

Use list comprehensions correctly


If you have a loop like this:

# 
An ugly, slow way to build a list
words = [&#39;her&#39;, &#39;name&#39;, &#39;is&#39;, &#39;rio&#39;]
alist = []
for word in words:
    alist.append(foo(word))


you can use a list Parse to rewrite:

words = [&#39;her&#39;, &#39;name&#39;, &#39;is&#39;, &#39;rio&#39;]
alist = [foo(word) for word in words]


Why do you do this? On the one hand, you avoid possible errors caused by correctly initializing the list. On the other hand, writing the code this way makes it look clean and tidy. For those with a functional programming background, using the map function may feel more familiar, but in my opinion this approach is less Pythonic.


Some other common reasons not to use list comprehensions:


1. Requires loop nesting. At this time you can nest the entire list parsing, or use a loop on multiple lines in the list parsing:

words = [&#39;her&#39;, &#39;name&#39;, &#39;is&#39;, &#39;rio&#39;]
letters = []
for word in words:
    for letter in word:
        letters.append(letter)


Use list parsing:

words = [&#39;her&#39;, &#39;name&#39;, &#39;is&#39;, &#39;rio&#39;]
letters = [letter for word in words
                  for letter in word]


Note: In a list comprehension with multiple loops, the loops are in the same order as if you were not using a list comprehension.


#2. You need a conditional judgment inside the loop. You just need to add this conditional to the list comprehension:

words = [&#39;her&#39;, &#39;name&#39;, &#39;is&#39;, &#39;rio&#39;, &#39;1&#39;, &#39;2&#39;, &#39;3&#39;]
alpha_words = [word for word in words if isalpha(word)]


One valid reason not to use list comprehensions is that you cannot use exception handling in list comprehensions. If some elements in the iteration may cause exceptions, you need to transfer possible exception handling through function calls in the list comprehension, or not use the list comprehension at all.

Performance defects

Checking content in linear time

Syntactically, checking whether a list or set/dict contains an element seems to make no difference on the surface, but Under the surface it is completely different. If you need to repeatedly check whether a certain data structure contains an element, it is best to use a set instead of a list. (If you want to associate a value with the element to be checked, you can use a dict; this also achieves constant check time.)

# 
假设以list开始
lyrics_list = [&#39;her&#39;, &#39;name&#39;, &#39;is&#39;, &#39;rio&#39;]
 
# 
避免下面的写法
words = make_wordlist() # 假设返回许多要测试的单词
for word in words:
    if word in lyrics_list: # 线性检查时间
        print word, "is in the lyrics"
 
# 
最好这么写
lyrics_set = set(lyrics_list) # 线性时间创建set
words = make_wordlist() # 假设返回许多要测试的单词
for word in words:
    if word in lyrics_set: # 常数检查时间
        print word, "is in the lyrics"


[Translator's Note: Python The elements of the set and the key values ​​of the dict are hashable, so the time complexity of searching is O(1). ]


It should be remembered: Creating a set introduces a one-time overhead, and the creation process will take linear time even if member checking takes constant time. So if you need to check members in a loop, it's better to take the time to create the set first, since you only need to create it once.

Variable leakage

Loop


Generally speaking, in Python, the scope of a variable is wider than you would in other languages Expect to be generous. For example: The following code in Java will not compile:

// Get the index of the lowest-indexed item in the array
// that is > maxValue
for(int i = 0; i < y.length; i++) {
    if (y[i] > maxValue) {
        break;
    }
}
// i在这里出现不合法:不存在i
processArray(y, i);


However, in Python, the same code will always execute smoothly and get the expected results:

for idx, value in enumerate(y):
    if value > max_value:
        break
 
processList(y, idx)


这段代码将会正常运行,除非子y为空的情况下,此时,循环永远不会执行,而且processList函数的调用将会抛出NameError异常,因为idx没有定义。如果你使用Pylint代码检查工具,将会警告:使用可能没有定义的变量idx。


解决办法永远是显然的,可以在循环之前设置idx为一些特殊的值,这样你就知道如果循环永远没有执行的时候你将要寻找什么。这种模式叫做哨兵模式。那么什么值可以用来作为哨兵呢?在C语言时代或者更早,当int统治编程世界的时候,对于需要返回一个期望的错误结果的函数来说为通用的模式为返回-1。例如,当你想要返回列表中某一元素的索引值:

def find_item(item, alist):
    # None比-1更加Python化
    result = -1
    for idx, other_item in enumerate(alist):
        if other_item == item:
            result = idx
            break
 
    return result

通常情况下,在Python里None是一个比较好的哨兵值,即使它不是一贯地被Python标准类型使用(例如:str.find [2])


外作用域


Python程序员新手经常喜欢把所有东西放到所谓的外作用域——python文件中不被代码块(例如函数或者类)包含的部分。外作用域相当于全局命名空间;为了这部分的讨论,你应该假设全局作用域的内容在单个Python文件的任何地方都是可以访问的。


对于定义整个模块都需要去访问的在文件顶部声明的常量,外作用域显得非常强大。给外作用域中的任何变量使用有特色的名字是明智的做法,例如,使用IN_ALL_CAPS 这个常量名。 这将不容易造成如下bug:

import sys
 
# 
See the bug in the function declaration?
def print_file(filenam):
    """Print every line of a file."""
    with open(filename) as input_file:
        for line in input_file:
            print line.strip()
 
if __name__ == "__main__":
    filename = sys.argv[1]
    print_file(filename)


如果你看的近一点,你将看到print_file函数的定义中用filenam命名参数名,但是函数体却引用的却是filename。然而,这个程序仍然可以运行得很好。为什么呢?在print_file函数里,当一个局部变量filename没有被找到时,下一步是在全局作用域中去寻找。由于print_file的调用在外作用域中(即使有缩进),这里声明的filename对于print_file函数是可见的。


那么如何避免这样的错误呢?首先,在外作用域中不是IN_ALL_CAPS这样的全局变量就不要设置任何值[3]。参数解析最好交给main函数,因此函数中任何内部变量不在外作用域中存活。


这也提醒人们关注全局关键字global。如果你只是读取全局变量的值,你就不需要全局关键字global。你只有在想要改变全局变量名引用的对象时有使用global关键字的必要。你可以在这里获取更多相关信息this discussion of the global keyword on Stack Overflow(http://stackoverflow.com/questions/4693120/use-of-global-keyword-in-python/4693170#4693170)。

代码风格

向PEP8致敬

PEP 8是Python代码的通用风格指南,你应该牢记在心并且尽可能去遵循它,尽管一些人有充分的理由不同意其中一些细小的风格,例如缩进的空格个数或使用空行。如果你不遵循PEP8,你应该有除“我只是不喜欢那样的风格”之外更好的理由。下边的风格指南都是从PEP8中摘取的,似乎是编程者经常需要牢记的。

测试是否为空

如果你要检查一个容器类型(例如:列表,词典,集合)是否为空,只需要简单测试它而不是使用类似检查len(x)>0这样的方法:

numbers = [-1, -2, -3]
# 
This will be empty
positive_numbers = [num for num in numbers if num > 0]
if positive_numbers:
    # Do something awesome


如果你想在其他地方保存positive_numbers是否为空的结果,可以使用bool(positive_number)作为结果保存;bool用来判断if条件判断语句的真值。

测试是否为None 

如前面所提到,None可以作为一个很好的哨兵值。那么如何检查它呢?

如果你明确的想要测试None,而不只是测试其他一些值为False的项(如空容器或者0),可以使用:

if x is not None:
    # Do something with x

如果你使用None作为哨兵,这也是Python风格所期望的模式,例如在你想要区分None和0的时候。

如果你只是测试变量是否为一些有用的值,一个简单的if模式通常就够用了:

if x:
    # Do something with x

例如:如果期望x是一个容器类型,但是x可能作另一个函数的返回结果值变为None,你应该立即考虑到这种情况。你需要留意是否改变了传给x的值,否则可能你认为True或0. 0是个有用的值,程序却不会按照你想要的方式执行。

译者注:


[1] 在Python2.x 中 range生成的是list对象,xrange生成的则是range对象;Python 3.x 废除了xrange,range生成的统一为range对象,用list工厂函数可以显式生成list;

[2] string.find(str)返回str在string中开始的索引值,如果不存在则返回-1;

[3] Do not set any value to the local variable name in the function in the external scope to prevent an error when calling the local variable inside the function and calling the variable with the same name in the external scope.

The above is the content of anti-patterns in Python programming. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn