Python interview_python
https://github.com/taizilongxu/interview_python
?
1 Python的函數(shù)參數(shù)傳遞
strings, tuples, 和numbers是不可更改的對(duì)象,而list,dict等則是可以修改的對(duì)象
?
2 Python中的元類(metaclass)
3 @staticmethod和@classmethod
python 三個(gè)方法,靜態(tài)方法(staticmethod),類方法(classmethod),實(shí)例方法
4 類變量和實(shí)例變量
類變量就是供類使用的變量,實(shí)例變量就是供實(shí)例使用的.
若是list,dict修改實(shí)例變量,類變量也改變。strings, tuples, 和numbers是不可更改的對(duì)象,故實(shí)例變量和類變量不同。
5 Python自省
自省就是面向?qū)ο蟮恼Z(yǔ)言所寫的程序在運(yùn)行時(shí),所能知道對(duì)象的類型.簡(jiǎn)單一句就是運(yùn)行時(shí)能夠獲得對(duì)象的類型.比如type(),dir(),getattr(),hasattr(),isinstance().
?
6 字典推導(dǎo)式
列表推導(dǎo)式(list comprehension)
In [39]: [x*x for x in range(10)]Out[39]: [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
2.7加入字典推導(dǎo)式
>>> strings = ['import','is','with','if','file','exception']????
>>> D = {key: val for val,key in enumerate(strings)}??
??
>>> D??
{'exception': 5, 'is': 1, 'file': 4, 'import': 0, 'with': 2, 'if': 3}??
7 單下劃線、雙下劃線
http://stackoverflow.com/questions/1301346/what-is-the-meaning-of-a-single-and-a-double-underscore-before-an-object-name
single underscore :?private
>>> class MyClass(): ... def __init__(self): ... self.__superprivate = "Hello" ... self._semiprivate = ", world!" ... >>> mc = MyClass() >>> print mc.__superprivate Traceback (most recent call last):File "<stdin>", line 1, in <module> AttributeError: myClass instance has no attribute '__superprivate' >>> print mc._semiprivate , world! >>> print mc.__dict__ {'_MyClass__superprivate': 'Hello', '_semiprivate': ', world!'}8 字符串格式化:%和.format
sub1 = "python string!" sub2 = "an arg"a = "i am a %s" % sub1 b = "i am a {0}".format(sub1)c = "with %(kwarg)s!" % {'kwarg':sub2} d = "with {kwarg}!".format(kwarg=sub2)print a # "i am a python string!" print b # "i am a python string!" print c # "with an arg!" print d # "with an arg!""hi there %s" % (name,) # supply the single argument as a single-item tuple9 迭代器和生成器
10?*args?and?**kwargs
*args,例如,它可以傳遞任意數(shù)量的參數(shù). ?You would use?*args?when you're not sure how many arguments might be passed to your function
**kwargs,允許你使用沒有事先定義的參數(shù)名.
*args表示任何多個(gè)無名參數(shù),它是一個(gè)tuple;**kwargs表示關(guān)鍵字參數(shù),它是一個(gè)dict。
https://stackoverflow.com/questions/3394835/args-and-kwargs/3394898#3394898
def foo(*args, **kwargs):print 'args = ', argsprint 'kwargs = ', kwargsprint '---------------------------------------'if __name__ == '__main__':foo(1,2,3,4)foo(a=1,b=2,c=3)foo(1,2,3,4, a=1,b=2,c=3)foo('a', 1, None, a=1, b='2', c=3) 輸出結(jié)果如下:args =? (1, 2, 3, 4)?
kwargs =? {}?
---------------------------------------?
args =? ()?
kwargs =? {'a': 1, 'c': 3, 'b': 2}?
---------------------------------------?
args =? (1, 2, 3, 4)?
kwargs =? {'a': 1, 'c': 3, 'b': 2}?
---------------------------------------?
args =? ('a', 1, None)?
kwargs =? {'a': 1, 'c': 3, 'b': '2'}?
---------------------------------------
?
# 當(dāng)調(diào)用函數(shù)時(shí)你也可以用 * 和 ** 語(yǔ)法 def star_operation(name, value, count):print("Name: {}, Value: {}, Count: {}".format(name, value, count))if __name__ == "__main__":# 它可以傳遞列表(或者元組)的每一項(xiàng)并把它們解包. 注意必須與它們?cè)诤瘮?shù)里的參數(shù)相吻合a_list = ["名字", "值", "計(jì)數(shù)器"]a_dict = {'a':1, 'b':2, 'b':3}star_operation(*a_list)star_operation(**a_dict.items())輸出:
Name: 名字, Value: 值, Count: 計(jì)數(shù)器 --------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-33-d38ee010e1b9> in <module>() 10 a_dict = {'a':1, 'b':2, 'b':3} 11 star_operation(*a_list) ---> 12 star_operation(**a_dict.items()) TypeError: star_operation() argument after ** must be a mapping, not list**后面必須是 mapping,映射
?
11 面向切面編程AOP和裝飾器
裝飾器的作用就是為已經(jīng)存在的對(duì)象添加額外的功能
# how decorators workdef makebold(fn):def wrapped():return "<b>" + fn() + "</b>"return wrapped def makeitalic(fn):def wrapped():return "<i>" + fn() + "</i>"return wrapped@makebold @makeitalic def hello():return "hello world"print hello() ## returns "<b><i>hello world</i></b>"函數(shù)即是對(duì)象
def shout(word="yes")return word.capitalize()+"!"print (shout()) # Yes!# As an object, you can assign the function to a variable like any other object scream = shout# Notice we don't use parenthese: we are not calling the fuction, # we are putting the function "shout" into the variable "scream". # It means you can then call "shout" from "scream": print (scream()) # Yes!# More than that, it means you can remove the old name 'shout', # and the function will still be accessible from 'scream'del shout try:print(shout()) except NameError, e:print(e) # "name 'shout' is not defined"print(scream()) # Yes!python: function can be defined inside another function / 函數(shù)能夠定義在其他函數(shù)內(nèi)。
Functions references:
1.can be assigned to a varible
2.can be defined in another function
def getTalk(kind="shout"):# We define functions on the flydef shout(word="yes"):return word.capitalize()+"!"def whisper(word="yes") :return word.lower()+"...";# Then we return one of themif kind == "shout":# We don't use "()", we are not calling the function, we are returning the function objectreturn shout else:return whisper# How do you use this strange beast?# Get the function and assign it to a variable talk = getTalk() # You can see that "talk" is here a function object: print(talk) #outputs : <function shout at 0xb7ea817c># The object is the one returned by the function: print(talk()) #outputs : Yes!# And you can even use it directly if you feel wild: print(getTalk("whisper")()) #outputs : yes...Decorator :
'wrappers', let you execute code before and after the function they decorate without modifying the function itself.
?
methods and functions are really the same. The only difference is that methods expect that their first argument is a reference to the current object (self).
方法和函數(shù)的唯一區(qū)別是,方法的第一個(gè)參數(shù)是對(duì)當(dāng)前對(duì)象的引用,self.
?
Python自帶的幾個(gè)裝飾器:property,staticmethod。。
Django 使用裝飾器來管理緩存和權(quán)限控制。
Twisted 用來實(shí)現(xiàn)異步調(diào)用。
?
12 鴨子類型
鴨子類型是動(dòng)態(tài)類型的一種風(fēng)格,在這種風(fēng)格中,一個(gè)對(duì)象有效的語(yǔ)義,不是由繼承自特定的類或?qū)崿F(xiàn)特定的接口,而是由當(dāng)前 方法和屬性的集合所決定。
例如,在不使用鴨子類型的語(yǔ)言中,我們可以編寫一個(gè)函數(shù),它接受一個(gè)類型為鴨的對(duì)象,并調(diào)用它的走和叫方法。在使用鴨子類型的語(yǔ)言中,這樣的一個(gè)函數(shù)可以接受一個(gè)任意類型的對(duì)象,并調(diào)用它的走和叫方法。如果這些需要被調(diào)用的方法不存在,那么將引發(fā)一個(gè)運(yùn)行時(shí)錯(cuò)誤。任何擁有這樣的正確的走和叫方法的對(duì)象都可被函數(shù)接受的這種行為引出了以上表述,這種決定類型的方式因此得名。
?
?
網(wǎng)絡(luò)
5 Post和Get
區(qū)別:
一個(gè)用于獲取數(shù)據(jù),一個(gè)用于修改數(shù)據(jù)。
?
轉(zhuǎn)載于:https://www.cnblogs.com/IDRI/p/6231535.html
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的Python interview_python的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 图论中的基础概念总结
- 下一篇: 批量下载小说网站上的小说(python爬