[Pin to top] Ten Python traps (1-3)

Source: Internet
Author: User

These traps are not necessarily language defects. However, the side effects of these languages often make new users fall down and even have experience.ProgramMembers will also be recruited. If you fully understand some of the core behaviors of Python, you may fall into it.

 

HereArticleSimilar to a guide for beginners of Python, it is better to understand these traps earlier than to encounter them before the deadline of the actual project:-) This is not a criticism of the Python language, as mentioned earlier, most of the traps are not due to language defects.


1. Inconsistent indentation

Okay, let's make it simple. However, new users who learn other languages that are "irrelevant with spaces" will be punished by Python due to their bad habits.

Solution: Uniform indentation. Use spaces or tabs instead of obfuscation. A good editor is helpful.


2. Assignment, alias, Object

I have learned some static languages, such as Pascal and C. I often assume that python variables and assignments work in the same way. At first glance, they look exactly the same:

A = B = 3A = 4 print a, B #4, 3

However, there is a problem with variable-length objects, because Python has different effects on variable-length objects and fixed-length objects.

 
A = [1, 2, 3] B = AA. append (4) print B # B is now [1, 2, 3, 4] as well

What's going on? A declaration statement A = [1, 2, 3] does two things: 1. create an object (a list) and assign it to [1, 2, 3]; 2. bind a to the Change volume in the local namespace. B = A binds B to the same list (referenced by. Once you realize this, understanding the behavior of a. append (4) is much simpler. It changes the value of the List referenced by A and B.

 

It is not true that a fixed-length object and a variable-length object behave differently when assigned values. When a = 3 B = A, both A and B reference the same object-an integer object with a value of 3, but because the integer object is unchangeable, you will not fall into a trap.

Solution: use the copy method and slice operator. python never implicitly copies the image. Read this.


3. + = Operator

A language such as C, ++ = is an operator that simplifies expressions. For example:

 
X + = 42;

Is the simplification of the following expression:

 
X = x + 42;

Therefore, you may think it is the same in Python. Of course, it looks exactly the same at the beginning:

 
A = 1a = a + 42 # A is 43a = 1a + = 42 # A is 43

However, for variable-length objects, x + = y and x = x + y are not exactly the same. Consider list:

 
>>> Z = [1, 2, 3] >>>> ID (z) 24213240 >>>> Z + = [4] >>> ID (z) 24213240 >>> z = z + [5] >>> ID (z) 24226184

X + = y changes the value of list in place, while X = x + y creates a new list and binds x again. A subtle difference may cause hard-to-track bugs.

 

More than that, when you mix fixed-length objects and variable-length objects, you will be surprised to find that:

 
>>> T = ([],) >>> T [0] + = [2, 3] traceback (most recent call last): file "<input> ", line 1, in? Typeerror: object doesn't support item assignment >>> T ([2, 3],)

Obviously, tuples do not support assigning values to elements. However, after using ++ = for them, the list in the tuples does change! The reason is still + = the value of the List is changed in place. However, the value assignment of tuples is not allowed. When an exception occurs, the list in the tuples has been changed in place.

 

This is a fatal trap I personally think.

Solution: Avoid using + =, or use it only when the integer is used. :-)


Original article: http://zephyrfalcon.org/labs/python_pitfalls.html

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.