The 10 most asked questions in the Python programmer interview

  • 2020-06-15 09:20:18
  • OfStack

Here are some of the most frequently asked questions in the Python programmer interview:

Python is a very popular programming language, and with the development of machine learning, cloud computing and other technologies in recent years, the job demand of Python is increasing. Here are 10 questions that Python interviewers often ask.

Class inheritance

There is the following 1 piece of code:


class A(object):
def show(self):
print 'base show'
class B(A):
def show(self):
print 'derived show' 
obj = B()
obj.show()

How to call the show method of class A.
The methods are as follows:


obj.__class__ = A
obj.show() 

The method of ___ points to the class object. Simply assign it the type A and call the method show, but don't forget to change it back.

The method object

Question: What code needs to be added in order for the following code to run?


class A(object):
def __init__(self,a,b):
self.__a = a
self.__b = b
def myprint(self):
print 'a=', self.__a, 'b=', self.__b
a1=A(10,20)
a1.myprint() 
a1(80)

Answer: In order for the object instance to be called directly, you need to implement the method with ___ 32EN__


class A(object):
def __init__(self, a, b):
self.__a = a
self.__b = b
def myprint(self):
print 'a=', self.__a, 'b=', self.__b
def __call__(self, num):
print 'call:', num + self.__a

new and init

What does the following code enter?


class B(object):
def fn(self):
print 'B fn'
def __init__(self):
print "B INIT"
class A(object):
def fn(self):
print 'A fn'
def __new__(cls,a):
print "NEW", a
if a>10:
return super(A, cls).__new__(cls)
return B()
def __init__(self,a):
print "INIT", a 
a1 = A(5)
a1.fn()
a2=A(20)
a2.fn()

The answer:


NEW 5
B INIT
B fn
NEW 20
INIT 20
A fn

Using the method with ___ 47en__, you can decide which object to return, that is, before creating the object, this can be used with the singleton, factory pattern of the design pattern. ___ the creation object is called.

Python list and dict are generated

What does this code output ?


ls = [1,2,3,4]
list1 = [i for i in ls if i>2]
print list1
list2 = [i*2 for i in ls if i>2]
print list2
dic1 = {x: x**2 for x in (2, 4, 6)}
print dic1
dic2 = {x: 'item' + str(x**2) for x in (2, 4, 6)}
print dic2
set1 = {x for x in 'hello world' if x not in 'low level'}
print set1

The answer:


[3, 4] 
[6, 8]
{2: 4, 4: 16, 6: 36}
{2: 'item4', 4: 'item16', 6: 'item36'}
set(['h', 'r', 'd'])

Global and local variables

What does this code output ?


num = 9
def f1():
num = 20
def f2():
print num 
f2()
f1()
f2()

The answer:


9
9

num is not a global variable, so each function gets its own copy of num, which you must declare with the global keyword if you want to modify num. Like this


obj.__class__ = A
obj.show() 
0

Exchange the values of the two variables
1 line of code exchanges two variable values


obj.__class__ = A
obj.show() 
1

The answer:


obj.__class__ = A
obj.show() 
2

The default method

The following code


class A(object):
def __init__(self,a,b):
self.a1 = a
self.b1 = b
print 'init'
def mydefault(self):
print 'default'
a1 = A(10,20)
a1.fn1()
a1.fn2()
a1.fn3()

Methods fn1/fn2/fn3 are undefined. Add code to call mydefault


obj.__class__ = A
obj.show() 
4

The answer:


obj.__class__ = A
obj.show() 
5

Method per ___ is called only if there is no defined method call. When the fn1 method passes in an argument, we can add one *args indefinite argument to the mydefault method for compatibility.


obj.__class__ = A
obj.show() 
6

Package management

There are 3 modules in one package, mod1.py, mod2.py, mod3.py.

Per 125en__.py and add:


obj.__class__ = A
obj.show() 
7

closure
Write a function that takes the integer argument n and returns a function that multiplies the arguments of the function with n and returns the result.

The answer:


def mulby(num):
def gn(val):
return num * val
return gn
zw = mulby(7)
print(zw(9));

performance

How slow is it to parse the following code


obj.__class__ = A
obj.show() 
9

Answer: python's str is an immutable object. With each iteration, a new str object is generated to store the new string. The larger the num, the more str objects are created, and the greater the memory consumption.


Related articles: