python常考题_Python语言基础考察点:python语言基础常见考题(一)
一、python是靜態還是動態類型?是強類型還是弱類型?
1、動態強類型語言(不少人誤以為是弱類型)
不要傻傻分不清
2、動態還是靜態指的是編譯期還是運行期確定類型
3、強類型指的是不會發生隱式類型轉換
若類型語言
強類型語言
4、python作為后端語言優缺點
1、膠水語言、輪子多、應用廣泛
2、語言靈活、生產力高
3、性能問題、代碼維護問題、python2/2兼容問題
動態語言一時爽、代碼重構火葬場
二、什么是鴨子類型
當看到一只鳥走起來想鴨子、有用起來像鴨子、叫起來也想鴨子、那么這只鳥就可以被稱為鴨子
1、關注點在對象的行為,而不是類型(duck typing)
2、比如 file、StringIO,socket對象都支持read/write方法(file like object)
2、在比如定義了 _iter_魔術方法的隊形可以用for迭代
代碼驗證
1、代碼
class Duck():
def quack(self):
print("gua gua")
class Person:
def quack(self):
print("我是人類,但我也會 gua gua gua")
def in_the_forest(duck):
duck.quack()
def game():
donald = Duck()
john = Person()
in_the_forest(donald)
in_the_forest(john)
print(type(donald))
print(type(john))
print(isinstance(donald,Duck))
print(isinstance(john,Person))
game()
2、輸出結果
duck_type.py
gua gua
我是人類,但我也會 gua gua gua
True
True
Process finished with exit code 0
三、什么是monkey patch?那些地方用到了?自己如何實現?
1、所謂的monkey patch就是運行時替換
2、比如gevent庫需要修改內置的socket
3、from gevent import monkey;monkey.patch_socket()
1、安裝gevent
1.在https://pypi.org/project/gevent/#files下載你需要的gevent版本,保存到一個文件夾中
2.在cmd中,cd到你Python的Script下進行安裝
3.cd 到你下載好的gevent 路徑
4.進入gevent路徑的系統盤中
5.pip install 下載好的gevent模塊名
2、gevent庫需要修改內置的socket
import socket
import gevent
print(socket.socket)
print("After momkey patch")
from gevent import monkey
monkey.patch_socket()
print(socket.socket)
import select
print(select.select)
monkey.patch_socket()
print("After momkey patch")
print(select.select)
輸出如下:
monkey_path.py
After momkey patch
After momkey patch
3、自己實現monkey patch
import socket
import gevent
print(socket.socket)
print("After momkey patch")
from gevent import monkey
monkey.patch_socket("After momkey patch")
print(socket.socket)
import select
print(select.select)
monkey.patch_socket()
print("After momkey patch")
print(select.select)
import time
print(time.time())
def _time():
return 1234
time.time = _time
print(time.time())
輸出結果如下:
monkey_path.py
After momkey patch
After momkey patch
1564107393.6268823
1234
Process finished with exit code 0
四、什么是自省?
運行時判斷一個對象的類型的能力
python一切皆對象、用type、id、isinstance獲取對象類型信息
ll = [1, 2, 3]
d = dict(a=1) #{a:1}
print(type(ll))
print(type(d))
print(isinstance(ll, list))
print(isinstance(d, dict))
def add(a, b):
if isinstance(a, int):
return a + b
elif isinstance(a, str):
return a.upper()+b
print(add(1, 2))
print(add('head', 'tail'))
輸出結果如下:
introspection.py
True
True
3
HEADtail
Process finished with exit code 0
Inspect模塊提供了更多獲取時對象信息的函數
ll = [1, 2, 3]
d = dict(a=1) #{a:1}
print(type(ll))
print(type(d))
print(isinstance(ll, list))
print(isinstance(d, dict))
def add(a, b):
if isinstance(a, int):
return a + b
elif isinstance(a, str):
return a.upper()+b
print(add(1, 2))
print(add('head', 'tail'))
print(id(ll))
print(id(d))
print(ll is d)
print(ll is ll)
輸出結果如下:
introspection.py
True
True
3
HEADtail
17718152
17742664
False
True
Process finished with exit code 0
五、什么是列表和字典推導
比如[i for i in range(10) if i % 2 == 0]
一種快速生成list/dict/set的方式,用來替代map/filter等
(i for i in range(10) if i % 2 == 0)返回生成器
a = ['a', 'b', 'c']
b =[1, 2, 3]
# d = {'a':1, 'b':2, 'c':3}
d = {}
for i in range(len(a)):
d[a[i]] = b[i]
print(d)
d = {k: v for k, v in zip(a,b)}
print(d)
輸出結果:
compresion.py
{'c': 3, 'b': 2, 'a': 1}
{'c': 3, 'b': 2, 'a': 1}
六、知道python之禪嗎?
Tim Peters 編寫的關于Python編寫的準則
import this
編程拿不準的時候可以參考
In [8]: import this
The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
總結
以上是生活随笔為你收集整理的python常考题_Python语言基础考察点:python语言基础常见考题(一)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 侠客风云传傅剑寒攻略
- 下一篇: 通过新版手机qq音乐我们怎样进入qq炫舞