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 classic Python interview questions?

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 classic interview questions of Python". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn what are the classic interview questions of Python.

Exchange variable values of Python interview questions (1)

Usually face to face with interns from time to time, most of the students have mastered Python at school. During the interview, students are required to implement a simple function to exchange the values of two variables. Most of the students give the following answers.

In fact, there are simpler and more Python-style implementations in Python, as follows

Compared with the former method, the latter method saves an intermediate variable and is superior to the former method in performance.

Let's take an in-depth analysis of the reason from the bytecode of Python.

Dis is a disassembly tool that translates Python code into bytecode instructions. The output here is as follows

As can be seen from the bytecode, the biggest difference between swap1 and swap2 is that the x and y values are interchanged through the two elements at the top of the ROT_TWO exchange stack in swap1, and the tmp variable is introduced into swap2, with one more operation of LOAD_FAST and STORE_FAST. Executing an ROT_TWO instruction is faster than executing an LOAD_FAST+STORE_FAST instruction, which is why swap1 performs better than swap2.

Python interview questions (2) the difference between is and =

When interviewing interns, when asked about the difference between is and = =, many students can't answer, and they don't know when the two will return the same and when they will return inconsistent. In this article, let's look at the difference between the two.

Let's first look at a few examples:

Why are some is and = = results the same and some different in the above output? Let's take a look at the official documentation for the explanations of is and =.

The official document says that is indicates whether the object identifier is consistent, that is, to compare whether the addresses of two objects in memory are the same, while = = is used to check whether the two objects are equal.

When we check an is b, we actually check id (a) = = id (b). When checking a = = b, the _ _ eq () _ _ method of object an is actually called, and a = = b is equivalent to a. Roomeqroome _ (b).

In general, if an is b returns True, that is, if an and b point to the same memory address, a = = b also returns True, that is, the values of an and b are also equal.

All right, after reading the above explanation, let's take a look at the previous examples.

It becomes clear after printing out id (a) and id (b). As long as the values of an and b are equal, a = = b returns True, while an is b returns True only if id (a) and id (b) are equal.

There is another question, why does an is b return True when both an and b are "hello" and an is b return False when both an and b are "hello world"?

This is because the string residency mechanism of Python works in the former case. For smaller strings, Python keeps a copy of its value to improve system performance, and points directly to that copy when creating a new string. So "hello" has only one copy in memory, and the id values of an and b are the same, while "hello world" is a long string and does not reside in memory. Objects are created in Python to represent an and b, so their values are the same but id values are different.

Students pointed out that the intern mechanism has nothing to do with string length. In interactive mode, each line of string literals will apply for a new string, but those with only uppercase and lowercase letters, numbers and underscores will be intern, that is, a dict is maintained to make these strings globally unique)

To sum up, is checks whether two objects point to the same block of memory space, while = = checks that their values are equal. As you can see, is is a stricter check than = =, and the return of True from is indicates that the two objects point to the same block of memory and must have the same value.

Seeing here, do you understand the difference between is and =?

Let's take a step further and think about the following question:

When comparing Python with None, why is it is None instead of = = None?

Friends will. You can leave a message in the comments area.

Python interview questions (3) variable objects and immutable objects

At the end of the last interview question: the difference between is and = in a Python interview:

When comparing Python with None, why is it is None instead of = = None?

This is because None is a singleton object in Python. If a variable is None, it must point to the same memory address as None. The call behind = = None is _ _ eq__, and _ _ eq__ can be overloaded. Here is an example of is not None but = = None

There are mutable objects and immutable objects in Python. A mutable object can be changed after it is created, but the address will not change, that is, the variable points to the original variable; after the immutable object is created, it cannot be changed, and if changed, it will point to a new object.

In Python, dict and list are mutable objects, while str, int, tuple and float are immutable objects.

Let's look at an example of a string.

In the above example, changing the value of the object a points to causes an exception to be thrown.

When executing a = a + "world", first evaluate the expression to the right of the equal sign and generate a new object assignment to the variable a, so that the object a points to has changed, and the value of id (a) is also different.

Let's look at an example of a list.

Modify the element and add the element in the face of a, and the variable a still points to the original object.

After assigning a to b, the variables b and a both point to the same object, so changing the element value of b also affects a.

The variable c is the return value of the slicing operation of b, which is equivalent to a shallow copy and generates a new object, so the object pointed to by c is no longer the object pointed to by b, and the operation on c will not change the value of b.

After understanding the difference between immutable and mutable objects above, let's look at an interesting question

Why does calling the add_member method of group2 affect the members of group1 when it is clear that group1 and group2 are different objects (with different id values)?

The secret is that the second parameter of the _ _ init__ function is the default parameter, which is generated when the function is created, and the object's cache is used for each call. When we check id (group1.mebers) and id (group2.members), we can find that they are the same.

Print (id (group1.members)) # output 140127132522040

Print (id (group2.members)) # output 140127132522040

So, group1.members and group2.members point to the same object, and changes to group2.members also affect group1.members.

Thank you for your reading, the above is the content of "what are the Python classic interview questions". After the study of this article, I believe you have a deeper understanding of what the Python classic interview questions have, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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