In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "what are the coding errors encountered in learning Python". The explanation in this article is simple and clear, easy to learn and understand. Please follow the ideas of Xiaobian and go deep into it slowly to study and learn "what are the coding errors encountered in learning Python" together!
Misunderstanding Python scope
Python parsing scope is based on the so-called LEGB rule, OCAL, Local, Closing, Global, Built-in Even though it seems simple, it was still a bit confusing for me at the time, for example, the following example:
For the above code, I want it to work and change the global variable x to print out 6. However, it suddenly became strange not to output as I thought, let's look at the changed code:
What the hell is going on? In a snippet, the global variable X gives an UnboundLocalError but it works when we just try to print it. The reason is related to scoping. When you assign a variable in a scope (such as a function scope), that variable becomes local to that scope and hides any similarly named variables in the outer scope. This is what happens when we do the first scenario x += 1. If we intend x to access global variables like a function, foo() can do the following:
By using the keyword global, the inner scope has access to variables declared in the global scope, meaning that there are no variables defined in any function. Similarly, we can use nonlocal to achieve a similar effect:
Nonlocalas global allows you to access variables from the external scope, but, for nonlocal, you can bind to objects in the parent scope or the global scope.
2. Modify the list when traversing the list
It is common among new Python developers and even some experienced developers. Although it may not seem obvious at times, in some cases we will have to modify the array we are currently iterating over, resulting in some behavior that is wrong with the code, or if we are lucky, we will receive an error and notice it. But let me give you an example of what I mean. Given an array that needs to be reduced to contain only even elements, try the following:
In the scenario described, when deleting elements of a list or array while iterating, we receive error messages due to attempts to access items that do not exist. This is a bad habit and should be avoided, there are better ways to implement similar functionality in Python, including list understanding:
The filter function can also be used to achieve the same functionality, although it works, but there are still people who think this is not Pythonic's method, I still agree with some, but I don't want to discuss them here. Let's continue:
3. Variable binding in closures
I asked some Python predecessors what they thought of the following code snippet:
For many people (myself included), the first time we encountered this problem, we thought the outcome would be:
But, in fact, the code ended up printing completely different results, and we were very confused as to why. It turns out Python performs late-binding behavior, according to which the inner function is called to query the value of the variable used in the closure. So, in our example, whenever any returned function is called, the value of is looked up in the surrounding range when i is called. The solution to this problem may seem tricky, but it works.
By passing values using the default arguments of lambda functions, i can generate functions to perform the desired behavior. I am very confused about this solution, but I still think it is not very elegant, but some people like it. If you know of an alternative solution to this problem, please let me know in the comments, I would love to read about it.
4. Name conflicts with Python standard library modules
When I first started learning, this problem was actually common, and even now, sometimes I make this mistake. The reason for this problem is to name one of your modules with the same name as the module in the standard library that comes with Python. (For example, you might have a module named email.py in your code that conflicts with a standard library module of the same name).
Maybe name conflicts by themselves don't cause any problems for your code, but sometimes we overwrite Python standard library features or modules that are later used in installed libraries and cause errors or misbehavior to conflict, which is a bad situation. A typical mistake is as follows:
By simply creating a list variable, we then interrupt access to the list function. And even if there are other ways to access it (e.g.__builtins__.list()), we should avoid using this name.
Thank you for reading, the above is the content of "what are the coding errors encountered in learning Python". After studying this article, I believe that everyone has a deeper understanding of what are the coding errors encountered in learning Python. The specific use situation still needs to be verified by practice. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!
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.