高级概念与包
函数式编程包operator
form operator import mul
可以替代
| 1
 | reduce(lambda a,b:a*b, range(1,1+n))
 | 
为
| 1
 | reduce(mul, range(1,1+n))
 | 
itemgetter(1) 可以替代 lambda fields: fields[1]:创建一个接受集合的函数,返回索引位1上的元素
attrgetter与itemgetter类似,它创建的函数根据名称提取对象的属性。如果把多个属性名传给attrgetter,它也会返回提取的值构成的元组。
methodcaller会自行创建函数,创建的函数会在对象上调用参数指定的方法。
| 12
 3
 4
 5
 6
 
 | from operator import methodcallers = 'The time is come'
 upcase = methodcaller('upper')
 upcase(s)
 
 Out[11]: 'THE TIME IS COME'
 
 | 
模块查找策略
目标:自动查找其他可用的*_promo函数
方式一:
| 1
 | promos = [globals()[name] for name in globals() if name.endswith('_promo') and name != 'best_promo']
 | 
方式二:将所有促销放在promotions模块
| 12
 
 | import inspectpromos = [func for name,func in inspect.getmembers(promotions, inspect.isfunction)]
 
 | 
inspect.getmembers用于捕获对象(这里是promotions模块)的属性,第二个参数是可选的判断条件(一个布尔值函数),这里只提取模块中的函数
闭包closure和偏函数Partial
内层函数+所引用的外层变量,称为闭包
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 
 | 
 
 
 
 
 def outer():
 msg = "这是一个局部变量"
 print("这是一个函数", msg)
 def inner():
 print("可以访问外部变量msg", msg)
 return msg
 return inner
 
 | 
- 在函数嵌套的前提下
- 内层函数引用了外层函数的变(包括参数)
- 外层函数又把内层函数当做返回值进行返回
闭包中,如果要修改引用的外层变量
- 需要使用 nonlocal 变量声明,表示非局部的
- 否则会被当做是闭包内,新定义的变量
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 
 | 
 from functools import partial
 def show2(name, msg):
 print(name, ": " , msg)
 
 s = partial(show2, msg="HI")
 s("tom")
 show2("jerry", "hello")
 
 
 tom :  HI
 jerry :  hello
 
 | 
python模块:profile,pstats
怪异问题避坑
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 
 | class Teas:def __init__(self, lst=[]):
 self.lst = lst
 
 def hyc_print(self):
 logger.info(self.lst)
 
 def __del__(self):
 pass
 
 
 if __name__ == '__main__':
 
 te1 = Teas()
 te1.lst.append(1)
 te1.hyc_print()
 
 te2 = Teas()
 te2.lst.append(2)
 te2.hyc_print()
 
 te1.hyc_print()
 logger.info(te1.lst is te2.lst)
 
 | 
| 12
 3
 4
 
 | 2020-05-29 17:14:40 multiprocessing_queue_iterator INFO:  [1]2020-05-29 17:14:40 multiprocessing_queue_iterator INFO:  [1, 2]
 2020-05-29 17:14:40 multiprocessing_queue_iterator INFO:  [1, 2]
 2020-05-29 17:14:40 multiprocessing_queue_iterator INFO:  True
 
 | 
| 12
 3
 4
 5
 6
 7
 8
 9
 
 | class Teas:def __init__(self, lst=None):
 self.lst = lst if lst is not None else []
 
 def hyc_print(self):
 logger.info(self.lst)
 
 def __del__(self):
 pass
 
 | 
| 12
 3
 4
 
 | 2020-05-29 17:15:43 multiprocessing_queue_iterator INFO:  [1]2020-05-29 17:15:43 multiprocessing_queue_iterator INFO:  [2]
 2020-05-29 17:15:43 multiprocessing_queue_iterator INFO:  [1]
 2020-05-29 17:15:43 multiprocessing_queue_iterator INFO:  False
 
 | 
扩展阅读
机器学习8大算法比较
| 1
 | https://mp.weixin.qq.com/s/0dT4BN01g0anVwyfjS-RVA
 | 
今年GitHub排名前20的Python机器学习开源项目
| 1
 | https://mp.weixin.qq.com/s/-WJ_S6CV7Cc14f4YzthPAQ
 | 
Python基础网站列表(有空再摘录)
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 
 | 1. 元组https://www.jianshu.com/p/b728648501a8
 2. 字典
 https://www.jianshu.com/p/8b51c9bf6d12
 3. 集合
 https://www.jianshu.com/p/75eb228b638e
 4. 列表
 https://www.jianshu.com/p/636314cf0126
 
 6.作用域
 https://www.jianshu.com/p/d8271c03a0f3
 
 8. 对象(属性限制-公有私有)
 https://www.jianshu.com/p/c7f6ecf07fbc
 9. 对象(属性限制-只读)
 https://www.jianshu.com/p/dd0e1487a4d6
 10. 对象(属性限制-只读优化)
 https://www.jianshu.com/p/f54e0a5af635
 11. 对象(系统内置方法-遍历操作)
 https://www.jianshu.com/p/bd87cca40d8b
 12. 内存管理机制-引用计数/垃圾回收/循环引用/弱引用
 https://www.jianshu.com/p/ef8a218c6b89
 13. 对象思想
 https://www.jianshu.com/p/0347ba667667
 14. 综合案例:封装/继承/多态
 https://www.jianshu.com/p/b35043c76f50
 15. 异常处理(错误和异常)
 https://www.jianshu.com/p/507d677e74a4
 16. 包/模块(概念及导入语法)
 https://www.jianshu.com/p/1d9100f8292a
 17. 包/模块(导入及其底层逻辑)
 https://www.jianshu.com/p/6a99e5e4c1b5
 18. 包/模块(三方包安装)
 https://www.jianshu.com/p/68477d5625fc
 19. 包/模块(创建和发布)
 https://www.jianshu.com/p/ee48fde9afd6
 20.
 
 
 | 
其他
    比如:
- 你要去做一个电商后台,存储着每件产品的 ID、名称和价格。现在需要根据商品 ID 找出价格,如何使用最合适的数据结构呢?
- 在 Python 中字典、集合都是经过高度性能优化的数据结构,如果采用列表来存储数据并进行查找,时间复杂度是多少?
- 换成字典呢?哪个更高效?事实上,采用不同数据结构存储十万数据,查找速度差异就有可能差出几千倍。
再比如:
- Python 中的协程和线程有什么区别?
- 生成器如何进化成协程?
- 并发编程中的 future 和 asyncio 有什么关系?
- 如何写出线程安全的高性能代码呢?

Python 基础入门
必学知识:【Python 基础数据结构】【Python 基础语法】【文件操作】【错误与异常处理】【Python 面向对象】【模块化】
第一步,你需要掌握 Python 的核心基础知识。当然,不同于其他基础教材,我不仅仅只讲基础概念、操作,同时也为你整理了很多进阶难度的知识,或是一些重难点、易错点等需要注意的地方。不仅可以让入门级的程序员查漏补缺,打捞基础,也能让有经验的程序员,重新从工程角度认识基础,升华理解。
Python 进阶核心知识
必学知识:【Python 协议】【Python 高级语法】【Python 正则表达式】【Python 并发编程】【垃圾回收机制】【项目实战】
第二步,进阶 Python 核心知识点,比如装饰器、并发编程等等。如果你的工作只是写 100 行以下的脚本程序,可能不怎么会用得到。但如果你做的是大型程序的开发,则非常有必要。
规范:编写高质量的 Python 程序
这部分着重于教你把程序写得更加规范、更加稳定。我在实际工作中见过不少程序员,会写程序,但写得实在有点“惨不忍睹”,导致最后调试起来错误不断,修改非常费劲儿。因此,我觉得用单独一个版块讲解这个问题非常有必要。
当然,我不会用一些似是而非的规范来说教,而是会用具体的编程操作和技巧,教你提高代码质量。比如,如何合理地分解代码、运用 assert,如何写单元测试等等。
Python 实战,串联整个知识体系:带你搭建量化交易系统
必学知识点:【RESTful】【Socket】【Pandas】【Numpy】【Kafka】【RabbitMQ】【MySQL】【Django】