Python developers face questions: 8 compulsory interview questions

'Gold and three silver four' people do not work do not know it, in fact, July is the switch to the peak of the interview, our students also joined the wave of army looking for work. I came up with based on experience and the actual situation for a moment in the interview Python, Python 8 question about the development of interview questions compulsory, there is little need partners, blackboard serious knock Kane!

Python developers face questions: 8 compulsory interview questions

 

1, the output code below what is? please explain.

def extendList(val, list=[]):
list.append(val)
return list
list1 = extendList(10)
list2 = extendList(123,[])
list3 = extendList( a )
print "list1 = %s" % list1
print "list2 = %s" % list2
print "list3 = %s" % list3

How to modify the definition of extendList can generate the following expected behavior?

The above code is output will be:

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

Many people will mistakenly believe list1 = [10], list3 = [ 'a'], because they think that every time extendList is called, the default value list of parameters will be set to []. But in fact the case that new the default list is created only once at that moment the function is defined.

When extendList call list is specific parameter is not specified, then the value of the set list will be used. This is because the expressions with default parameters are calculated when the function is defined, instead of being calculated at the time of the call. Thus list3 list1 and are operating on the same default list (basis). And list2 is operated (calculated) in a separate list. (Owned by passing empty list as a list of parameter values).

extendList definition can be modified as follows.

Although, create a new list, there is no specific list of parameters.

The following code may be able to produce the desired results.

def extendList(val, list=None):
if list is None:
list = []
list.append(val)
return list

By the above modification, the output becomes:

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

 

Python developer interview questions: 2, following output of this code will be what is? please explain.

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

How do you change the definition of the above multipliers to produce the desired results?

The above result is output code [6, 6, 6, 6] (not what we want to [0, 2, 4, 6]).

Reason for the above problems is to delay the closure of the binding Python. This means that an internal function is called, the value of the parameter to find in the closure. Accordingly, when any function of Multipliers () is called to return the value of i will be in the range around look. At that time, the function returns regardless of whether the calls, for the cycle has been completed, i was given a final value of 3.

Thus, each time the function returns the passed value is multiplied by 3, since the upper sections of the code is passed over the value 2, which is eventually returned 6 (2 * 3). As it happens, "The Hitchhiker's Guide to Python" also pointed out that, in correlation with lambdas function also has a knowledge widely misunderstood, but with this case is not the same. Function created by the lambda expression is nothing special place, and it is actually the creation of functional def same.

Here are some ways to solve this problem.

One solution is to use Python generators.

def multipliers():
for i in range(4): yield lambda x : i * x

Another solution is to create a closure, using the default function to bind immediately.

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

There are kinds of alternative is to use partial function:

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

3, following output of this code will be what is? please 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 output will be:

1 1 1
1 2 1
3 2 3

So many people are confused or surprised why the last line of output is 323 instead of 32 1. Why change parent.x but also changes the value of child2.x? But at the same time it does not change the value of Child1.x?

The key to this answer is that in Python, class variables are passed in the form of an internal dictionary.

If a variable name is not found in the dictionary under the current class. In the more advanced classes (such as its parent class) are found in the dedicated search until the variable name reference. (If the reference variable name is not found in their class and higher class, it will lead to a property error.)

Thus, x = 1 is set in the parent class, the class so that the variable x (with value 1) can be cited to its class and its subclasses. That's why the first print statement output is 111

Thus, if any one of its subclasses value is overwritten (e.g. say, when we execute the statement Child1.x = 2), this value is only modified in the subclass. This is why the second print statement output is 121

Eventually, if the value has been modified in the parent class (for example, says that when we execute the statement Parent.x = 3), this change will affect those who have not overwrite the value of the subclass (in this case is Child2) this is why the third print statement output is 323

4, the following code output at Python2 What will be the result? please explain.

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 results of what will be different at Python3? (Of course, assuming the print statement is converted into the syntax Python3)

In Python2, the above code output will be

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

By default, Python 2 automatically performs calculation shaping if both are integers. Therefore, 5/2 the result is 2, and the result was 2.5 5./2

Note that in Python2, you can override this behavior by adding the following references.

from future import division

Note also that the // operator will always perform plastic surgery division, regardless of the type of operator. That is why even in the results of Python 2 5.0 // 2.0 is 2.0. However, in Python3, there is no such properties,

For example, in a case where both ends are shaped, it does not perform the division shaping

Thus, in Python3, the result will be as follows:

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

 

NOTE: In the Python 3, / operator is doing floating point division, but do // divisible (without remainder commercially i.e., 10 @ 3 such a result, it is 3, is cut from the remainder removed, and (-7 ) // 3 is the result of -3. the algorithm and many other programming languages ​​is not the same, need to pay attention, they will value the integer division in the direction of 0, while in the Python 2, / is divisible, and that is Python 3 // in the same operator)

 

5, the output of the following code will be what is?

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

The following code output [], IndexError error does not occur. Like, as expected, try index exceeds the number of members to get a list of members.

For example, attempts to obtain list [10] and later a member of, will lead IndexError.

However, try to get a slice of the list, index began to exceed the number of members will not produce IndexError, but only returns an empty list.

This is a particularly disgusting incurable diseases, because time is running no error occurs, causing bug is difficult to be traced.

6, consider the following code fragment:

list = [ [ ] ] * 5
list # output?
list[0].append(10)
list # output?
list[1].append(20)
list # output?
list.append(30)
list # output?

2,4,6,8 line will output what results? Explain.

Output results are as follows:

[[], [], [], [], []]
[[10], [10], [10], [10], [10]]
[[10, 20], [10, 20], [10, 20], [10, 20], [10, 20]]
[[10, 20], [10, 20], [10, 20], [10, 20], [10, 20], 30]

Explained as follows:

The first output line intuitively easily understood, for example, list = [[]] * 5 is simply to create five empty list. However, understanding the expression list = [[]] * 5 The key point is that it is not to create a list that contains a list of five independent, but it is a list that contains a list of five of the same references created. Only by understanding this, we can better understand the following output.

list [0] .append (10) 10 added to the first list.

But because all five list is the same list of references, so the result will be:

[[10], [10], [10], [10], [10]]

Similarly, list [1] .append (20) 20 attached to the second list. But also due to the same five list is a list of references, so the output is now:

[[10, 20], [10, 20], [10, 20], [10, 20], [10, 20]]

In contrast, list.append (30) is on the whole outer listing additional new element, thus producing the result is: [[10, 20], [10, 20], [10, 20], [10, 20] , [10, 20], 30].

7、Given a list of N numbers。

Given a list comprising N digits.

Single list formula to generate a new list, the list contains only a value that satisfies the following conditions:

(A) an even value

(B) the original list element is the even slices.

For example, if the list [2] contains the value is an even number. Then this value should be included in the new list of them. Because the even sequence of figures in the original list (2 even) on. However, if the list [3] contains an even number,

That number should not be included in the new list them, because on it odd sequence in the original list.

Simple solution to this problem is as follows:

[x for x in list[::2] if x%2 == 0]

For example, given the following list:

list = [ 1 , 3 , 5 , 8 , 10 , 13 , 18 , 36 , 78 ]

List formula [x for x in list [:: 2] if x% 2 == 0] of the result,

[10, 18, 78]

Step this expression work is the first step in removing the even numbers slice,

A second step wherein all odd removed.

8, given the following dictionary subclass, the following code can run it? why?

class DefaultDict(dict):
def __missing__(self, key):
return []
d = DefaultDict()
d[ florp ] = 127

Able to run.

When the deletion key, perform DefaultDict classes, instances dictionary automatically instantiate the series.

About Python developers face questions we have no better add to it? Have you met any interesting thing in Python interview? Welcome Message!

Guess you like

Origin www.cnblogs.com/cherry-tang/p/11076280.html