专栏名称: 编程派
Python程序员都在看的公众号,跟着编程派一起学习Python,看最新国外教程和资源!
目录
相关文章推荐
51好读  ›  专栏  ›  编程派

Python面试必须要看的15个问题

编程派  · 公众号  · Python  · 2017-02-13 11:36

正文

请到「今天看啥」查看全文


本文由EarlGrey@编程派编译,转载请务必注明作者及出处。 点击“阅读原文”查看答案即可。

引言

想找一份Python开发工作吗?那你很可能得证明自己知道如何使用Python。下面这些问题涉及了与Python相关的许多技能,问题的关注点主要是语言本身,不是某个特定的包或模块。每一个问题都可以扩充为一个教程,如果可能的话。某些问题甚至会涉及多个领域。

我之前还没有出过和这些题目一样难的面试题,如果你能轻松地回答出来的话,赶紧去找份工作吧!

问题1

到底什么是Python?你可以在回答中与其他技术进行对比(也鼓励这样做)。

问题2

补充缺失的代码

  1. def print_directory_contents(sPath):

  2.    """

  3.    这个函数接受文件夹的名称作为输入参数,

  4.    返回该文件夹中文件的路径,

  5.    以及其包含文件夹中文件的路径。

  6.    """

  7.    # 补充代码

问题3

阅读下面的代码,写出A0,A1至An的最终值。

  1. A0 = dict(zip(('a','b','c','d','e'),(1,2,3,4,5)))

  2. A1 = range(10)

  3. A2 = [i for i in A1 if i in A0]

  4. A3 = [A0[s] for s in A0]

  5. A4 = [i for i in A1 if i in A3]

  6. A5 = {i:i*i for i in A1}

  7. A6 = [[i,i*i] for i in A1]

问题4

Python和多线程(multi-threading)。这是个好主意码?列举一些让Python代码以并行方式运行的方法。

问题5

你如何管理不同版本的代码?

问题6

下面代码会输出什么:

  1. def f(x,l=[]):

  2.    for i in range(x):

  3.        l.append(i*i)

  4.    print lf(2)f(3,[3,2,1])f(3)

问题7

“猴子补丁”(monkey patching)指的是什么?这种做法好吗?

问题8

这两个参数是什么意思: args, *kwargs?我们为什么要使用它们?

问题9

这些是什么意思:@classmethod, @staticmethod, @property?

问题10

阅读下面的代码,它的输出结果是什么?

  1. class A(object):

  2.    def go(self):

  3.        print "go A go!"

  4.    def stop(self):

  5.        print "stop A stop!"

  6.    def pause(self):

  7.        raise Exception("Not Implemented")class B(A):

  8.    def go(self):

  9.        super(B, self).go()

  10.        print "go B go!"class C(A):

  11.    def go(self):

  12.        super(C, self).go()

  13.         print "go C go!"

  14.    def stop(self):

  15.        super(C, self).stop()

  16.        print "stop C stop!"class D(B,C):

  17.    def go(self):

  18.        super(D, self).go()

  19.        print "go D go!"

  20.    def stop(self):

  21.        super(D, self).stop()

  22.        print "stop D stop!"

  23.    def pause(self):

  24.        print "wait D wait!"class E(B,C): pass

  25. a = A()

  26. b = B()

  27. c = C()

  28. d = D()

  29. e = E()

  30. # 说明下列代码的输出结果

  31. a.go()

  32. b .go()

  33. c.go()

  34. d.go()

  35. e.go()

  36. a.stop()

  37. b.stop()

  38. c.stop()

  39. d.stop()

  40. e.stop()

  41. a.pause()

  42. b.pause()

  43. c.pause()

  44. d.pause()

  45. e.pause()

问题11

阅读下面的代码,它的输出结果是什么?

  1. class Node(object):

  2.    def __init__(self,sName):

  3.        self._lChildren = []

  4.        self .sName = sName

  5.    def __repr__(self):

  6.        return "".format(self.sName)

  7.    def append(self,*args,**kwargs):

  8.        self._lChildren.append(*args,**kwargs)

  9.    def print_all_1(self):

  10.        print self

  11.        for oChild in self._lChildren:

  12.            oChild.print_all_1()

  13.    def print_all_2(self):

  14.        def gen(o):

  15.            lAll = [o,]

  16.            while lAll:

  17.                oNext = lAll.pop(0)

  18.                lAll.extend(oNext._lChildren)

  19.                 yield oNext

  20.        for oNode in gen(self):

  21.            print oNode

  22. oRoot = Node("root")

  23. oChild1 = Node("child1")

  24. oChild2 = Node("child2")

  25. oChild3 = Node("child3")

  26. oChild4 = Node("child4")

  27. oChild5 = Node("child5")

  28. oChild6 = Node("child6")

  29. oChild7 = Node("child7")

  30. oChild8 = Node("child8")

  31. oChild9 = Node( "child9")

  32. oChild10 = Node("child10")

  33. oRoot.append(oChild1)

  34. oRoot.append(oChild2)

  35. oRoot.append(oChild3)

  36. oChild1.append(oChild4)

  37. oChild1.append(oChild5)

  38. oChild2.append(oChild6)

  39. oChild4.append(oChild7)

  40. oChild3.append(oChild8)

  41. oChild3.append(oChild9)

  42. oChild6.append(oChild10)

  43. # 说明下面代码的输出结果

  44. oRoot.print_all_1()

  45. oRoot.print_all_2()

问题12

简要描述Python的垃圾回收机制(garbage collection)。

问题13

将下面的函数按照执行效率高低排序。它们都接受由0至1之间的数字构成的列表作为输入。这个列表可以很长。一个输入列表的示例如下:[random.random() for i in range(100000)]。你如何证明自己的答案是正确的。

  1. def f1(lIn):

  2.    l1 = sorted(lIn)

  3.    l2 = [i for i in l1 if i0.5]

  4.    return [i*i for i in l2]

  5. def f2(lIn):

  6.    l1 = [i for i in lIn if i0.5]

  7.    l2 = sorted(l1)

  8.    return [i*i for i in l2]

  9. def f3(lIn):

  10.    l1 = [i*i for i in lIn]

  11.    l2 = sorted(l1)

  12.    return [i for i in l1 if i0.5*0.5)]

问题14

你有过失败的经历吗?

问题15

你有实施过个人项目吗?

真的?

结语

我给出的这些问题时,有意涉及了多个领域。而且答案也是特意写的较为啰嗦。在编程面试中,你需要展示自己对语言的理解,如果你能简要地说清楚,那请务必那样做。我尽量在答案中提供了足够的信息,即使是你之前从来没有了解过这些领域,你也可以从答案中学到些东西。我希望本文能够帮助你找到满意的工作。

加油!


题图:pexels,CC0 授权。

点击 阅读原文 ,查看更多 Python 教程和资源。







请到「今天看啥」查看全文