In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "what are the five code odors in Python". In daily operation, I believe many people have doubts about what the five code odors are in Python. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the questions of "what are the five code odors in Python?" Next, please follow the editor to study!
Variable default parameter
Using default parameters is a common operation in Python, where you can set a predetermined value and choose to change it when called. This is useful when setting text, numbers, or Boolean values because it helps avoid a long list of parameters with redundant values.
However, setting a variable value to the default parameter can be dangerous and can lead to bug. Take a look at the following example:
Def addElements (a = []): a.append (5) return aaddElements () # [5] addElements () # [5,5]
The same function gives different results each time it is called. The problem with variable defaults in Python is that they are calculated only once when the function is defined. Using variant values every time a function is called can cause unexpected problems because tracking function calls is really troublesome.
Therefore, it is safer to use None as the default value and assign variable variables in the function, because you don't end up with maintainability issues and use variable default parameters only when you determine that you need them.
Select `range` instead of `rangerate`
Python's for loop is not the most common way to write code, but it is sometimes needed. Now that the for loop in Python does not run like other languages, you may instinctively write traditional-style range (len ()) in an unconventional way, as follows:
Names = ["a", "b", "c"] for i in range (len (names)): print (I, names [I])
It is quite common to repeat loops based on C-style indexes, but this is an inappropriate practice. It forces you to access elements through explicit index variables, so not only is the Python feature not obvious, but it also has readability problems.
Using enumerator provides the advantage of a tuple responsible for tracking both index values and elements. In addition to being simpler and more optimized, it also provides an optional second parameter to set the value.
For I, name in enumerate (names): print (I, name)
Ignore built-in functions and excessive loops
The loop is not unavailable, but it can result in lengthy conditional code when a transformation operation is applied to it. In this case, it is important not to ignore the built-in functions that are already available, such as map () filter () and reduce (). More importantly, Python provides list parsing, which is clearly the most Python-specific alternative loop approach.
Nested for loops are another typical example of bad code. Python programmers can easily get shot when doing pattern matching or running multiple iterations together. The following code will look ugly once you add a few more lines:
For x in listA: for y in listB: r.append (x, y)
Using itertools not only improves performance, but also makes it simpler and clearer. See how neat the above code is in itertools.product ():
For x, y in itertools.product (listA,listB): r.append ((x, y))
By using the product above, it can also be easily passed to other higher-order functions. When iterating over multiple lists at the same time, it's also good to use the zip () function (or enumerator if you need an index).
Analysis of abuse list
List parsing can flexibly create lists and is powerful, but it is easy to be misused or abused. Take a look at some cases.
(1) excessive list parsing when not needed
Often, we start to indulge in using list parsing in order to try something fancy, not to really need it. For example, in simple cases, you can use a list constructor:
Names = ["A", "B", "C"] [x.lower () for x in names] # use this list (map (str.lower, names))
(2) use list parsing when it is not actually stored
List parsing helps you easily define and create lists, but they are always stored in memory. If you do not use system processes, it is possible to damage a large amount of data. Therefore, using a generator expression is a better choice because it loads one value at a time as needed.
Nested analysis also requires attention, as it can lead to readability problems, and it is important to know when to use it and when to fall back to the for loop.
Like Boolean flag parameters and global variables
Boolean is the easiest data type to learn. In Python, providing named parameters makes your job much easier. However, they can easily generate complex code with nested if else blocks and cause readability problems. There are hidden dependencies in multiple Boolean relationships, which will cause some problems. Therefore, it is better to use enumerations rather than Dobble logic. The Enum data type is extensible to ensure a better code structure.
Global variables are cumbersome in all languages, and so is Python. Although we do need to use them sometimes, it can be dangerous to misuse them as a shortcut to pass or access data because it is variable.
Tracking its status can be tricky because you never know who might change it. If you start using global variables everywhere, naming conflicts can cause namespaces to be adversely affected.
At this point, the study on "what is the smell of the five major codes in Python" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.