Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the bad habits of Python code

2025-04-10 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 bad habits of Python code". The explanation in this article is simple and clear, easy to learn and understand. Please follow the idea of Xiaobian and go deep into it slowly to study and learn "what are the bad habits of Python code" together!

The default parameter of a function is a list.

Consider the following example:

Normally, this is what we expect.

But when we execute the code, we get nothing but this.

Not what you expected. Why? Because Python lists are mutable objects, and function arguments are passed references, when func is called a second time, b already has element 1, and b ends up with two elements 1 and 2.

The func method in the example is relatively simple, and when you find a problem, you can find the root cause simply by looking at it. However, if it is in a more complex method, you may inadvertently ignore this point and run into some inexplicable problems.

So when we want to set default parameters for functions, don't use mutable objects.

Change the code above to this and it will be OK.

Expected results achieved after implementation

II. File operation

It's easy to write similar code when you're new to Python and doing file operations.

This is no problem, but we do not have to manually maintain file resources, such as closing such operations to the context manager to do well.

It doesn't look very refreshing.

Catch all exceptions

As above, sometimes it's easy to catch an Exception in order to get things done quickly. This may catch exceptions such as KeyboardInterrupt (CTRL + C) or Assertion Error. Catching ambiguous exceptions can also cause inexplicable problems in our programs, and we should avoid doing so.

The accurate approach is to catch specific exceptions such as ValueError, AttributeError, TypeError, etc. according to the context, and then do appropriate error handling, such as printing logs.

Ignore Python's for…else syntax

In development, it is easy to encounter similar requirements, in a list, to determine whether a particular element exists. For example, the following code determines if there are odd numbers in the list.

Here, we use an identity is_odd_exist, which defaults to False. When an odd number is found, set it to True and jump out of the loop. There's nothing wrong with writing this way, but we can do it another way.

Python's for…else syntax, which executes statements in else when the for loop ends normally (i.e., it doesn't end with a break).

Here, we use a different way than other languages such as C, PHP, etc., to accomplish the same function, and it seems that the code is also a lot simpler.

V. Traversing dictionaries using keys

A Python beginner's buddy might be able to easily write such code.

Again, this is fine, but it doesn't seem intuitive. When traversing the dictionary, you can actually extract key information directly, like this:

Thank you for reading, the above is "Python code bad habits what" content, after the study of this article, I believe that we have a deeper understanding of Python code bad habits what this problem, the specific use of the situation also 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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report