python 面試必讀


總結了10道題的考試側重點,供參考: 
1.How are arguments passed – by reference of by value? 
考的是語法,基本功,雖說python程序員可以不用關心堆棧指針那些頭疼的東東,但傳引用和傳值的區別還是必需清楚的。個人感覺從python中一切都是對象的角度看,第一題問傳值還是傳引用其實是考官有意看面試者是不是概念清楚,真正希望考生回答的是哪些對象傳遞到函數中是只讀的或者說不可變的。 

2.Do you know what list and dict comprehensions are? Can you give an example? 
印象中 list and dict comprehensions 語法比一般循環的實現簡潔了很多很多,不過說實在的由於之前用多了其他語言,后來轉用python的話真不太習慣。 

兩個常見的例子: 

>>> print {i : chr(65+i) for i in range(4)} 
{0 : 'A', 1 : 'B', 2 : 'C', 3 : 'D'} 

S = [2 * x for x in range(101) if x ** 2 > 3] 

3.What is PEP 8? 
第3題 PEP8是眾多pythoner們使用的編碼規范,很多公司已經直接使用PEP8了,如果連看還沒看過的朋友請馬上看下吧:http://www.python.org/dev/peps/pep-0008/ 
面試中如果你還能說說PEP7,以及使用它們的感受,相信考官肯定會給你加分。 

4.Do you use virtual environments? 
是不是有獨立配開發環境的能力。 virtual environments 給開發人員進行多版本、多環境下的開發調試帶來了極大的便利,很難想象沒有Virtualenv,多版本開發有多頭大,不是不成,別跟自己過不去不是。 
推薦: 
http://pythoncentral.org/how-to-install-virtualenv-python/ 
https://pypi.python.org/pypi/virtualenv 

5.Can you sum all of the elements in the list, how about to multuply them and get the result? 
>>> data = ['5', '4', '9'] 
>>> sum(int(i) for i in data) 
18 
>>>reduce(lambda x, y: x+int(y), data, 0) 
18 

>>> data = [5, 4, 9] 
>>> sum(data) 
18 
>>> reduce(lambda x, y: x*y, data) 
180 
>>> 

6.Do you know what is the difference between lists and tuples? Can you give me an example for their usage? 
list 和 tuple 的不同首先是的語法,相同點都是類似數組的容器,都可以保存任意類型的對象,不同是tuple中的元素不可變,list中的元素是可以改變的,因此list比tuple多了一些管理其中保存元素的方法,如append, insert, pop, remove, sort, reverse等。 

值得一提的是 Python 2.6 開始有了tuple 的子類 collections.namedtuple 可以通過元素的名字訪問tuple了 (因為它具有 attribute 語法) ,當然還可以進過元素索引下標訪問。 

常見的例子是: 

import collections 
freturn = collections.namedtuple('freturn', 'fee fie foo fum') 

def f(): 
... 
return freturn(fee, fie, foo, fum) 

#讀取返回值: 

r = f() 
print r.fie # 相當於print r[1] , 但是提高了代碼的可讀性 

7.Do you know the difference between range and xrange? 
使用中最大的區別是在內存上,另外還可以看下stackoverflow里的解答:http://stackoverflow.com/questions/94935/what-is-the-difference-between-range-and-xrange 

值得注意的是python3里的range函數相當於python2的xrange。 

8.Tell me a few differences between Python 2.x and 3.x 
希望你對新老產品都有所關注, 2/3的區別參考: 
http://docs.python.org/release/3.0/whatsnew/3.0.html 
http://www.python.org/dev/peps/pep-3000/ 

9.What are decorators and what is their usage? 
@classmethod大家一定都用,關於Decorators的說明(by Bruce Eckel): 
http://www.artima.com/weblogs/viewpost.jsp?thread=240808 

http://www.python.org/dev/peps/pep-0318/ 
Decorators for Functions and Methods 

10.The with statement and its usage. 
典型的pythonic語法 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM