标签:版本 fuse 优缺点 安装 soc unless ever 强类型 big
不要傻傻分不清
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
1、所谓的monkey patch就是运行时替换
2、比如gevent库需要修改内置的socket
3、from gevent import monkey;monkey.patch_socket()
1.在https://pypi.org/project/gevent/#files下载你需要的gevent版本,保存到一个文件夹中 2.在cmd中,cd到你Python的Script下进行安装 3.cd 到你下载好的gevent 路径 4.进入gevent路径的系统盘中 5.pip install 下载好的gevent模块名
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>
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
运行时判断一个对象的类型的能力
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
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}
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语言基础常见考题(一)
标签:版本 fuse 优缺点 安装 soc unless ever 强类型 big
原文地址:https://www.cnblogs.com/luoahong/p/11251661.html