In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces you what are the most common Python development mistakes, the content is very detailed, interested friends can refer to, hope to be helpful to you.
Python is an easy-to-learn programming language with concise and clear syntax and a rich and powerful class library. Unlike most other programming languages that use curly braces, it uses indentation to define statement blocks.
In daily work, Python developers are easy to make some small mistakes, these mistakes are easy to avoid, the big podium teacher summed up the 10 most common mistakes made by Python developers, let's take a look, I wonder if you have been shot.
(1) abuse the expression as the default value of the function parameter
Python allows developers to specify a default value for function parameters. Although this is a feature of the language, it can easily lead to confusion when the parameters are variable. For example, the following function definition:
In the above code, once the foo () function is called repeatedly (no bar parameter is specified), it will always return 'bar', because no argument is specified, so foo () will be given [] each time it is called. Let's take a look at the result:
Solution:
(2) misuse of class variables
Let's take a look at the following example:
This makes sense:
Do it again:
It just changed A.x, why did C.X change with it?
In Python, class variables are processed internally as dictionaries and follow the method parsing order (MRO). In the above code, because attribute x is not found in class C, it looks for its base class (only An in the above example, although Python supports multiple inheritance). In other words, C has no x attribute of its own and is independent of A, so referencing C.x is actually referring to A.x.
(3) specify incorrect parameters for the exception
Suppose you have the following code in the code:
The problem here is that the except statement does not need this way to specify the exception list. However, in Python 2.x, except Exception,e is usually used to bind the second parameter in an exception for further inspection. Therefore, in the above code, the IndexError exception is not caught by the except statement, and the exception * * is bound to a parameter called IndexError.
The correct way to catch multiple exceptions in an exception statement is to specify * * parameters as a tuple that contains all the caught exceptions. At the same time, the as keyword is used to ensure the portability of *, which is supported by both Python 2 and Python 3.
(4) misunderstanding the scope of Python rules
The scope resolution of Python is based on LEGB rules, which are Local, Enclosing, Global and Built-in, respectively. In fact, there are some tricks in this parsing method, as shown in the following example:
Many people will be surprised that when they add a parameter statement to the body of the working function, they report a UnboundLocalError error in the previously working code (click here for a more detailed description).
It is easy for developers to make such mistakes when using lists. Take a look at the following example:
Why does foo2 fail and foo1 works properly?
The answer is the same as the previous example, but with some subtleties. Foo1 is not assigned to lst, but foo2 is assigned. Lst + = [5] is actually lst = lst + [5], trying to assign a value to lst (therefore, assume that Python is in local scope). However, we are looking for a value assigned to lst that is based on lst itself, which has not yet been determined.
(5) modify the traversal list
The following code is clearly wrong:
It is a very low-level error to delete the list when traversing. No one with a little experience will commit a crime.
Make changes to the above code to execute correctly:
(6) how to bind variables in closures
Look at the following example:
The result you expect is:
In fact:
Are you very surprised! This is mainly due to the late binding behavior of Python, which is used in the closure while internal functions are calling it.
Solution:
(7) create circular module dependencies
Suppose you have two files, a.py and b.py, and import them separately, as follows:
In a.py:
In b.py:
First, let's try to import a.py:
You may be surprised that you can work well. After all, we did do a circular import here, shouldn't there be a problem?
The mere existence of a circular import is not a problem with Python itself, and if a module is imported, Python will not try to re-import. According to this, each module may encounter some problems at run time when trying to access functions or variables.
What happens when we try to import b.py (we didn't import a.py previously):
Something went wrong, and the problem here is that during the import of b.py, you also try to import a.py, which calls f () and tries to access B. X. But b.x is not defined.
You can do this by simply modifying the g () function that b.py imports into a.py:
Everything works fine whenever imported:
(8) conflict with the name of Python standard library module
Python has a very rich module library and supports "out of the box". Therefore, naming conflicts can easily occur if you do not deliberately avoid them. For example, you may have an email.py module in your code, which is likely to conflict with the standard library module that comes with Python because of the same name.
(IX) failure to deal with the difference between Python2.x and Python3.x in accordance with the regulations
Take a look at foo.py:
It works well in Python 2:
But in Python 3:
Solution:
Run the result in Py3k:
In the Python recruitment guide, there are many notes and discussions that need to be paid attention to when porting code between Python 2 and Python 3, you can take a look.
(10) abuse of _ _ del__ method
For example, here is a file called mod.py:
Next, you do the following in the another_mod.py file:
You will get an AttributeError exception.
When the interpreter is turned off, the global variables of the module are all set to None. So, in the above example, when _ _ del__ is called, foo has all been set to None.
A good solution is to use atexit.register () instead. By the way, when the program is finished, the handler you registered will stop working before the interpreter closes.
The code that fixes the above problem:
Under the premise of normal termination of the program, this implementation provides a clean and reliable way to invoke any function that needs to be cleaned up.
Python is a powerful and flexible programming language with many mechanisms and patterns to greatly improve productivity. As with any language or software tool, there is a restrictive understanding or appreciation of its capabilities, some of which do more harm than good, and sometimes bring some pitfalls.
On the most likely to make Python development mistakes are shared here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
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.