随笔分类 -  Python基础

摘要:1. 装饰器的示例1 参考: Fluent_Python的笔记,http://www.cnblogs.com/allen2333/p/8848010.html 装饰器本质上就是一个python函数,他可以让其他函数在不需要做任何代码变动的前提下,增加额外的功能,装饰器的返回值也是一个函数对象。(符合 阅读全文
posted @ 2018-05-09 21:04 Rocin 阅读(145) 评论(0) 推荐(0)
摘要:反射就是通过字符串的形式,导入模块;通过字符串的形式,去模块寻找指定函数,并执行。利用字符串的形式去对象(模块)中操作(查找/获取/删除/添加)成员,一种基于字符串的事件驱动。 1. 反射 ,用于方法调用 library.py user.py 如果func有100个,我们的代码就很长,所以采用反射的 阅读全文
posted @ 2018-05-09 20:33 Rocin 阅读(194) 评论(0) 推荐(0)
摘要:1. Anaconda 自带Conda,可以自定义环境 2. Pycharm 3. zeal API离线查看,类似于Dash 阅读全文
posted @ 2018-05-06 17:26 Rocin 阅读(155) 评论(0) 推荐(0)
摘要:留坑 参考: 1. https://en.wikipedia.org/wiki/Coroutine 2. https://zh.wikipedia.org/wiki/%E5%8D%8F%E7%A8%8B 3. http://www.cnblogs.com/xybaby/p/6323358.html 阅读全文
posted @ 2018-04-25 09:57 Rocin 阅读(421) 评论(0) 推荐(0)
摘要:1. 参考: 1. https://en.wikipedia.org/wiki/Callback_(computer_programming) 2. https://developer.mozilla.org/en US/docs/Glossary/Callback_function 3. http 阅读全文
posted @ 2018-04-24 14:16 Rocin 阅读(1553) 评论(0) 推荐(0)
摘要:多进程(multiprocessing) 参考: 1. https://docs.python.org/3.6/library/multiprocessing.html 1. 多进程概念 multiprocessing is a package that supports spawning proc 阅读全文
posted @ 2018-04-23 22:07 Rocin 阅读(1009) 评论(0) 推荐(0)
摘要:1. 缓冲区(此处用阻塞队列充当),解决消费者和生产者强耦合问题。(生产者和消费者不直接通信) 2. 通过平衡生产者线程和消费者线程,来提高程序整体处理数据速度。 3. 在并发编程中该模式能解决大多数并发问题。 4. 例子1. 生产者生产一次,每个消费者消费一次 5. 例子2. 生产者和消费者动态生 阅读全文
posted @ 2018-04-23 21:45 Rocin 阅读(542) 评论(0) 推荐(0)
摘要:参考: 1. https://stackoverflow.com/questions/49859287/what is the need of threading lock when cpython has gil 2. https://stackoverflow.com/questions/400 阅读全文
posted @ 2018-04-22 12:56 Rocin 阅读(742) 评论(0) 推荐(0)
摘要:在c.py中 阅读全文
posted @ 2018-04-22 09:29 Rocin 阅读(6521) 评论(0) 推荐(0)
摘要:1. zip函数生成一个由 元组构成的生成器 ,元组中的元素来自参数传入的各个可迭代对象。一旦有一个输入的可迭代对象耗尽,zip函数会立即停止生成值,而且不发出警告。使用zip_longest解决。 2. 使用for循环迭代元素不用处理索引变量,还能避免很多缺陷,需要一些特殊的使用函数来辅助。其中一 阅读全文
posted @ 2018-04-17 13:46 Rocin 阅读(235) 评论(0) 推荐(0)
摘要:例子1. python中实现hashable map函数是惰性的,和生成器表达式一样,创建一个生成器,按需产出结果,节省内存 另外: 例子2. 计算整数0~5累计异或的三种方式 2.1 for循环 2.2 reduce + lambda 2.3 reduce + operator(代替lambda) 阅读全文
posted @ 2018-04-17 13:18 Rocin 阅读(341) 评论(0) 推荐(0)
摘要:1. dir()查看类的方法和属性 查看slice类的方法和属性 2.help() 查看某个方法的文档 查看slice类中的indices方法 阅读全文
posted @ 2018-04-17 10:57 Rocin 阅读(13072) 评论(0) 推荐(1)
摘要:参考: 1. Fluent_Python P430 2. wiki 这里说的协议是什么?是让Python这种动态类型语言实现 多态 的方式。 1. 在面向对象编程中,协议是 非正式 的接口,是一组方法,但只是一种文档,语言不对施加特定的措施或者强制实现。 2. 虽然协议是非正式的, 在Python中 阅读全文
posted @ 2018-04-17 10:38 Rocin 阅读(2375) 评论(0) 推荐(0)
摘要:参考 1. https://docs.python.org/3/library/copy.html?highlight=copy%20copy copy.copy 2. https://en.wikipedia.org/wiki/Object_copying Shallow_copy Fluent 阅读全文
posted @ 2018-04-12 09:25 Rocin 阅读(253) 评论(0) 推荐(0)
摘要:参考 https://docs.python.org/3/library/functions.html?highlight=int int If x is not a number or if base is given, then x must be a string, bytes, or byt 阅读全文
posted @ 2018-04-11 16:47 Rocin 阅读(260) 评论(0) 推荐(0)
摘要:参考 https://stackoverflow.com/questions/2970608/what are named tuples in python 先上例子,用tuple和namedtuple分别表示点(x, y),然后计算两个点的距离 1. 用tuple表示点,从例子可见计算两个点的距离 阅读全文
posted @ 2018-04-10 23:28 Rocin 阅读(323) 评论(0) 推荐(0)
摘要:cd进要启动服务器的目录 python m http.server 7800 (Python 3.x) python m SimpleHTTPServer 7998 (Pyhton2.x) 阅读全文
posted @ 2018-04-09 13:13 Rocin 阅读(629) 评论(0) 推荐(0)
摘要:列表推导式(list comprehension) Python2: Python2中for关键词之后的赋值操作可能会影响context中的同名变量。Python3不存在此问题。 Python3: 1. 原本context中的变量x的值被保留了。 2. 列表推导式也创建了正确的列表。 阅读全文
posted @ 2018-04-03 20:56 Rocin 阅读(252) 评论(0) 推荐(0)
摘要:dist packages 和 site packages的区别 Reference: https://stackoverflow.com/questions/9387928/whats the difference between dist packages and site packages h 阅读全文
posted @ 2018-04-03 11:38 Rocin 阅读(445) 评论(0) 推荐(0)