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 7 mistakes that should not be made in Python?

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article shows you the 7 mistakes that should not be made in Python, which are concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

Introduction

Python is a relatively easy language to master, but because it assumes a lot of things, it's very easy to make mistakes. In addition, these errors may be ignored for a long time. Before my other four rookie Python mistakes.

Avoid these novice Python errors

They may work, but they may be better.

Today, I will share more mistakes that are easy to make in Python 3.8.

№ 1vl + =

In many cases, one of the mistakes I certainly make is to get the + = Operand. It seems too easy to assume that you have to add with two separate operands, one for addition and the other for assertion. Fortunately, Python (and many other programming languages) supports operands that can perform both mathematical and assertive operations.

Instead of:

X = x + 5

Do:

X + = 5

№ 2: position argument symbol

In most modern scripting languages, two main types of parameters can be used as arguments to a function:

Location and keywords.

Before the release of python 3.8, there was no way to represent location parameters, which made the default values of location parameters basically impossible. However, because the change was updated in Python 3.8, we can use it. You should use them because positional parameters are the basis for high-quality Python code with excellent performance. If there's one thing your code needs to compensate for with Python, especially when machine learning, it's probably performance.

Keyword parameters are evaluated once when the function is defined. This may be particularly bad for the performance of recursive algorithms. Also, if you want to write a cost function, you must absolutely use the location parameter. This is not to say that they should not be used at all, but it is best to use positional parameters whenever possible.

Instead of:

Def numbers (custom, five=5,ten=10,fifteen=15): total = five + ten + fifteen + custom return (total)

Do:

Def numbers (custom, five=5,ten=10,fifteen=15): total = five + ten + fifteen + custom return (total)

It is also important to remember that this feature is quite new, so the concept of using only positional parameters to improve performance will be limited to Python 3.8 +

№ 3: initialization returns

Although this is certainly not common and may be a fairly big rookie action, I have seen people try to perform returns in their initializers. In the object-oriented world of Python, the _ _ init__ function is called every time a class is instantiated. Initialization functions are useful for setting variables, moving data, and construction types. Powerful, heavy responsibility, Python's _ _ init__ function is applied to class initialization, not monitoring, not looping, not directly executed, only for class initialization. And please (my experienced people are doing this!)

Do not return init!

It doesn't make any sense!

№ 4: circular dependency

When writing a large Python package, each segment can achieve similar goals, and in general, your packages will depend on each other. This can cause big problems and can lead to something called "circular dependencies". When this happens, you may encounter problems when different locations around the module try to access each other.

№ 5: spaces or indents?

As you know, Python uses indentation instead of delimiters to include code in other code. For many people, this makes the code easier to read and write, although subjectively I personally despise it. However, in other languages, I usually tend to use indentation (the Tab key) to indent code. However, according to PEP8, four spaces should be used for indentation. To be honest, this is a principle I only follow when working on projects with a lot of people. For me, I don't regard spaces or indents as important as long as they are consistent, but in a formal sense, you should use spaces. Having said that, it may be a good idea to follow PEP8 standards when writing actual code for a job or large projects that require many other users to follow.

№ 6: block iteration

When executing an iterative for loop, writing a full-length for loop makes it easy to express your ideas in your code. However, in general, if you need to perform an operation that occupies only one line, it is best to cycle through one line. This will not only make your code more beautiful and concise, but also improve performance.

Instead of:

For i in x: I + = 5

Do:

[I + 5 for i in x]

№ 7: internship string

With regard to the last mistake to consider, I would like to talk about internship strings. In some cases, Python tries to reuse immutable objects, and string internships are a practical example of this. All strings of length 1 are masked. There is nothing in the string except ASCII characters, numbers and underscores.

Here, I try to create two different objects an and b, but that's not what Python does. Python is actually a pointer from b to a, rather than generating a new string. However, if we add ASCII characters:

Therefore, keep this in mind when using immutable strings, because it undoubtedly brings me a lot of trouble.

The above are the seven mistakes that should not normally be made in Python. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

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