V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX  ›  linw1995  ›  全部回复第 6 页 / 共 6 页
回复总数  107
1  2  3  4  5  6  
2017-07-30 17:16:42 +08:00
回复了 coordinate 创建的主题 Python 如何使用 Python 登陆 v2ex?
(⊙﹏⊙)跟一楼一样发现了这个问题,换了表单名,可是还是不行。
2017-07-27 16:29:19 +08:00
回复了 xoxo419 创建的主题 程序员 你的电脑杀毒软件装的是哪个款式?
当然是火绒啊
2017-07-26 22:04:29 +08:00
回复了 bccber 创建的主题 Python 用类作字典的 Key 能搞不?
>>> from collections.abc import Hashable
>>> mutable = [list, bytearray, set, dict]
>>> immutable = [int, float, complex, str, tuple, frozenset, bytes]
>>> all(isinstance(x(), Hashable) for x in immutable)
True
>>> all(issubclass(x, Hashable) for x in immutable)
True
>>> any(isinstance(x(), Hashable) for x in mutable)
False
>>> any(issubclass(x, Hashable) for x in mutable)

基本类型里,不可变的都是可 hash 的。无论是有序的 Tuple 还是无序的 frozenset,因为其是不可变的,都是可 hash 的。所以都可以当 mapping 的 key。
若是要自定义可 hash 的对象,就一定要实现__hash__、__eq__两个方法。
若是不实现,也可以 hash。这是因为类缺省__hash__、__eq__两个方法,但是其依据是 id 值,那么结果就不是我们想要的。

参考:
https://wiki.python.org/moin/DictionaryKeys#User_Defined_Types_as_Dictionary_Keys
http://www.laurentluce.com/posts/python-dictionary-implementation/
2017-07-18 22:10:19 +08:00
回复了 billion 创建的主题 Python Python 的异步如何应用在普通操作上?
@billion
仔细看下文档就知道了,如果要回调,就使用 ThreadPoolExecutor().submit(func, args),会返回一个 future 对象,他有 add_done_callback 方法。文档在这,https://docs.python.org/dev/library/concurrent.futures.html#concurrent.futures.Future.add_done_callback
想看例子的,可以看下这篇文章
http://masnun.com/2016/03/29/python-a-quick-introduction-to-the-concurrent-futures-module.html

concurrent.futures 是个特别容易使用的异步库,哈哈。twisted 什么的太复杂了,async/await 也挺不错的,写法也还简单,不过题主问的问题是要把普通操作变成异步的……那就是把普通函数做成异步的,我是这么理解的,所以就用这个最合适了。
2017-07-18 17:34:53 +08:00
回复了 billion 创建的主题 Python Python 的异步如何应用在普通操作上?
用 ThreadPoolExecutor 就可以咯
2017-07-18 17:34:11 +08:00
回复了 billion 创建的主题 Python Python 的异步如何应用在普通操作上?
```python
from concurrent.futures import ThreadPoolExecutor as Pool
filenames = [...]
def readFile(filename):
with open(filename, encoding='utf-8') as f:
content = f.read()
return content # or do what you want

with Pool(10) as executor:
results = executor.map(readFile, filenames)
for result in results:
print(result)
```
2017-05-16 15:10:17 +08:00
回复了 peneazy 创建的主题 程序员 图灵社区的勘误去哪了
对呀怎么不见,是不是要联系该书的执行编辑?
1  2  3  4  5  6  
关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   4992 人在线   最高记录 6543   ·     Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 · 17ms · UTC 09:20 · PVG 17:20 · LAX 02:20 · JFK 05:20
Developed with CodeLauncher
♥ Do have faith in what you're doing.