5 good Python interview question Answers and analysis

  • 2020-07-21 09:03:03
  • OfStack

The main content of this paper is to share several T questions in the Python interview, and give the answers and analysis, as follows.

In this paper, the original is 5 Great Python Interview Questions, at the same time thank you @ the tortoise said my omissions, no source tag, also praised its careful, want to see the article at the same time, everyone can see the original, because everyone's understanding is not 1, the original most helpful, I translated a lot of article 1 for the purpose of his later to find convenient; 2 is used as an index to read the original text more quickly. I hope you can read the original text.

Question 1: What would the output of the following code be ? Say your answer and explain.


class Parent(object):
 x = 1

class Child1(Parent):
 pass

class Child2(Parent):
 pass

print Parent.x, Child1.x, Child2.x
Child1.x = 2
print Parent.x, Child1.x, Child2.x
Parent.x = 3
print Parent.x, Child1.x, Child2.x

The answer

The output of the above code is:


1 1 1
1 2 1
3 2 3

What's confusing or surprising to you is that the output on the last line is 3, 2, 3 instead of 3, 2, 1. Why does changing the value of ES19en. x change the value of Child2.x, but not the value of Child1.x?

The key to this answer is that in Python, class variables are handled internally as dictionaries. If the name of a variable is not found in the dictionary of the current class, the ancestor class (such as the parent class) is searched until the referenced variable name is found (an AttributeError exception is thrown if the referenced variable name is not found in its own class or in the ancestor class).

Therefore, setting x = 1 in the parent class causes the class variable X to have a value of 1 in the reference class and in any of its subclasses. This is because the output of the first print statement is 1, 1, 1.

Then, if any of its subclasses overrides the value (for example, we execute the statement Child1.x = 2), then the value is changed only in the subclass. That's why the output of the second print statement is 1, 2, 1.

Finally, if the value is changed in a parent class (for example, we execute the statement Parent.x = 3), this change affects the value in any subclass that does not override the value (in this case, the affected subclass is Child2). That's why the third print output is 3, 2, 3.

Question 2: What would the output of the following code be ? Say your answer and explain it?


def div1(x,y):
 print("%s/%s = %s" % (x, y, x/y))

def div2(x,y):
 print("%s//%s = %s" % (x, y, x//y))

div1(5,2)
div1(5.,2)
div2(5,2)
div2(5.,2.)

The answer

The answer really depends on whether you are using Python 2 or Python 3.

In Python 3, the expected output is:


5/2 = 2.5
5.0/2 = 2.5
5//2 = 2
5.0//2.0 = 2.0

In Python 2, however, the output of the above code would be:


5/2 = 2
5.0/2 = 2.5
5//2 = 2
5.0//2.0 = 2.0

By default, Python 2 automatically performs integer calculations if both operands are integers. As a result, 5/2 has a value of 2, while 5./2 has a value of ' ' '2.5' '.

Note, however, that you can override this 1 behavior in Python 2 (to achieve the same result you want in Python 3, for example) by adding the following imports:


from __future__ import division

Also note that the "double underscore" (//) operator divisible 1, regardless of the type of operand, which is why the 5.0//2.0 value is 2.0.

Note: in Python 3, the/operator does floating-point division, while // does divisible (that is, the quotient has no remainder, such as 10/3/3, which results in a remainder of 3 and is truncated, whereas (-7) // 3 gives a remainder of -3. This algorithm is different from many other programming languages, and it should be noted that their divisible operations will be evaluated in the direction of 0. In Python 2, / is divisible, just like the // operator in Python 3,

Question 3: What will the following code output ?


list = ['a', 'b', 'c', 'd', 'e']
print list[10:]

The answer

The above code will output [] and will not result in 1 IndexError.

As one would expect, an attempt to access a member that exceeds the index value of the list will result in IndexError (for example, access to list[10] for the above list). However, slices attempting to access a list that start with an index that exceeds the number of list members will not result in IndexError and will simply return an empty list.

One annoying little problem is that it causes bug, and this problem is hard to track because it doesn't cause errors at run time.

Question 4: What would the output of the following code be ? Say your answer and explain it?


def multipliers():
 return [lambda x : i * x for i in range(4)]

print [m(2) for m in multipliers()]

How would you modify the definition of multipliers to produce the desired result

The answer

The output of the above code is [6, 6, 6, 6] (not [0, 2, 4, 6]).

The reason for this is late binding due to the late binding of Python's closure, which means that variables in the closure are looked up when the internal function is called. So the result is that when any function returned by multipliers() is called, at which point the value of i is looked up in the surrounding scope when it was called, by which time the for loop is complete regardless of which returned function is called, i ends up with a value of 3, so each returned function multiplies has a value of 3. So a value equal to 2 is passed into the above code, and they will return a value of 6 (for example: 3 x 2).

(By the way, as pointed out in The Hitchhiker 's Guide to Python, there is a common misconception about the lambda expression. The function created with 1 lambda expression is not special, and the function created with 1 regular def will behave the same way.

There are two ways to solve this problem.

The most common solution is to create a closure and bind its parameters immediately by using default parameters. Such as:


def multipliers():
 return [lambda x, i=i : i * x for i in range(4)]

Another option is that you can use the functools.partial function:


from functools import partial
from operator import mul

def multipliers():
 return [partial(mul, i) for i in range(4)]

Question 5: What would the output of the following code be ? Say your answer and explain it?


1 1 1
1 2 1
3 2 3
0

How would you modify the definition of extendList to produce the desired result

The output of the above code is:


1 1 1
1 2 1
3 2 3
1

Many people mistakenly think that list1 should be equal to [10] and list3 should be equal to ['a']. Consider that the parameter list will be set to its default value every time extendList is called [].

What actually happens, though, is that the new default list is created only once when the function is defined. When extendList is subsequently invoked without a specified list parameter, it USES the same list. This is why an expression is evaluated with default arguments when a function is defined, not when it is called.

Therefore, list1 and list3 are the same list of operations. ' ' 'list2, on the other hand, operates on a separate list it creates (by passing its own empty list as the value of the list' 'argument).

The definition of the extendList function can be changed as follows, but when no new list parameter is specified, a new list is always started, which is more likely to be the expected behavior of 1.


1 1 1
1 2 1
3 2 3
2

Using this improved implementation, the output would be:


list1 = [10]
list2 = [123]
list3 = ['a']

conclusion

As for the interview, how can I make a good impression on the interviewer? For example, people test you this part of the program output results is what, you can not only answer, if you can point out the code in the implementation of the function is unchanged, what can be optimized, 1 will let the examiner eyes 1 bright. Either way, you need a solid foundation of knowledge.

That's all the answers and analysis to five good Python interview questions. Interested friends can continue to refer to other related topics in this site, if there is any deficiency, welcome to comment out. Thank you for your support!


Related articles: