Python interviews often ask 10 questions

Python a lot of people looking for work, the interview often pit mining on the basis of knowledge

Python is a very popular programming language, along with machine learning in recent years, the development of cloud computing and other technologies, Python jobs demand higher and higher. Here I have collected 10 Python interviewer frequently asked questions for your reference study.
ps: Also a lot of people in the process of learning Python, often because there is no one good tutorial or guide leading their own easy to give up, and I built a Python exchange dress: a long time and its military while a stream of thought (numbers under homonym) conversion can be found, there are new Python tutorial projects can take, I do not understand the problem more exchanges with people inside will solve Oh!

1, class inheritance

The following piece of code:

A class (Object):  
    DEF show (Self):  
        Print 'Base show'   
class B (A):  
    DEF show (Self):  
        Print 'derived show'  
 
obj = B ()  
obj.show () 
how to show call class A way to go.

Methods as below:

.__ class__ = Aobj.show obj () 
__class__ method points to the class object, only to give him an assignment type A, then call the method show, but I remember running out modifications back.

2, the method of the object

Question: In order to make the following piece of code to run, need to increase what code?

class A (Object): 
    DEF the __init __ (Self, A, B): 
        Self .__ A = A 
        Self .__ B = B 
    DEF myPrint (Self): 
        Print 'A =', Self .__ A, 'B =', Self .__ B 
A = A1 (10,20) 
a1.myprint () 
A1 (80) 
answer: In order to allow the object instance can be called directly, need to implement the method __call__

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 
3、new和init

What the following code output?

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() 
答案:

. 5 NEW 
B the INIT 
B Fn 
NEW 20 is 
the INIT 20 is 
A Fn 
use __new__ method may decide to return that object, that is, before creating the object, which can be used in a single embodiment, the factory design pattern mode. __init__ is to create an object is called.

4, Python list and generating dict

What the following 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 
答案:

[. 3,. 4]   
[. 6,. 8] 
{2:. 4,. 4: 16,. 6: 36} 
{2: 'ITEM4',. 4: 'item16',. 6: 'item36'} 
SET ([ 'H', ' R & lt ',' D ']) 
. 5, global and local variables

What the following code output?

=. 9 NUM   
DEF F1 ():  
    NUM = 20 is  
 
DEF F2 ():  
    Print NUM  
 
F2 ()  
F1 ()  
F2 () 
Answer:

9  

num is not a global variable, so each function got my copy num, num if you want to change, you must use the global keyword statement. For example follows

=. 9 NUM  
DEF F1 ():  
    Global NUM  
    NUM = 20 is  
DEF F2 ():  
   Print NUM  
F2 ()  
F1 ()  
F2 ()  
# Prints:  
#. 9  
# 20 is 
. 6, the exchange value of two variables

Exchange line of code values ​​of two variables

. 8 = A  
B =. 9 
Answer:

(A, B) = (B, A) 
. 7, the default method

Following code

A class (Object):  
    DEF the __init __ (Self, A, B):  
        self.a1 = A  
        self.b1 B =  
        Print 'the init'  
    DEF MyDefault (Self):  
        Print 'default'  
 
A1 = A (10,20)  
A1. Fn1 ()  
a1.fn2 ()  
a1.fn3 () 
method fn1 / fn2 / fn3 are not defined, the code is added, there is no defined methods call mydefault function, the above code to be output

defaultdefaultdefault 
答案:

A class (Object): 
    DEF the __init __ (Self, A, B):  
        self.a1 = A  
        self.b1 B =  
        Print 'the init'  
    DEF MyDefault (Self):  
        Print 'default'  
    DEF __getattr __ (Self, name):  
        return Self .mydefault  
 
a1 = a (10, 20)  
a1.fn1 ()  
a1.fn2 ()  
a1.fn3 () 
method __getattr__ only when there is no defined method invocation, is calling him. When passing parameters fn1 method, we can add a * args variable parameters to be compatible mydefault method.

class A(object):  
    def __init__(self,a,b):  
        self.a1 = a  
        self.b1 = b  
        print 'init'  
    def mydefault(self,*args):  
        print 'default:' + str(args[0])  
    def __getattr__(self,name):  
        print "other fn:",name  
        return self.mydefault 
 
 
a1 = A(10,20)  
a1.fn1(33)  
a1.fn2('hello')  
a1.fn3(10) 
8、包管理

A bag with three modules, mod1.py, mod2.py, mod3.py, but use from demopack import * import module, how to ensure that only mod1, mod3 was introduced.

The answer: increase __init__.py file, and increases in the file:

= __all__ is [ 'MOD1', 'mod3'] 
. 9, closure

Write a function takes an integer n, a return function, the function is a function of function parameters and returns the result of multiplying n.

answer:

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

Parsing the following code in which slow

DEF strtest1 (NUM):  
    str = 'First'  
    for I in (NUM) Range:  
        str + = "X-"  
    return str 
Answer: python of str is immutable, each iteration, generates a new str object to store the new string, the greater the num, the more str object is created, the greater memory consumption.
More than ten questions will be yet? --- Also a lot of people in the process of learning Python, often because there is no one good tutorial or guide leading their own easy to give up, and I built a Python exchange dress: a long time and its military while a stream of thought (Digital under homonym) conversion can be found, there are new Python tutorial projects can take, I do not understand the problem more exchanges with people inside will solve Oh!

Text and images in this article from the network with their own ideas, only to learn, exchange, not for any commercial purposes, belongs to original author, if any questions, please contact us for treatment.



Guess you like

Origin www.cnblogs.com/chengxuyuanaa/p/12144870.html