一、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 <class '__main__.Duck'> <class '__main__.Person'> 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 <class 'socket.socket'> After momkey patch <class 'gevent._socket3.socket'> <built-in function select> After momkey patch <built-in function select>
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 <class 'socket.socket'> After momkey patch <class 'gevent._socket3.socket'> <built-in function select> After momkey patch <built-in function select> 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 <class 'list'> <class 'dict'> 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 <class 'list'> <class 'dict'> 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!