小编Bog*_*kiy的帖子

Hash for Python中的lambda函数

我正在尝试获取lambda函数的哈希值.为什么我得到两个值(8746164008739和-9223363290690767077)?为什么lambda函数的哈希值不总是一个值?

>>> fn = lambda: 1
>>> hash(fn)
-9223363290690767077
>>> fn = lambda: 1
>>> hash(fn)
8746164008739
>>> fn = lambda: 1
>>> hash(fn)
-9223363290690767077
>>> fn = lambda: 1
>>> hash(fn)
8746164008739
>>> fn = lambda: 1
>>> hash(fn)
-9223363290690767077
Run Code Online (Sandbox Code Playgroud)

python hash lambda python-2.7

25
推荐指数
4
解决办法
1893
查看次数

Python 3 urllib.request.urlopen

urllib.request.urlopen如果response.status_code不是200,我怎样才能避免例外?现在它提出URLErrorHTTPError基于请求状态.

有没有其他方法来使用python3基本库进行请求?

如果能获得响应头status_code != 200

python exception urllib httprequest python-3.4

7
推荐指数
3
解决办法
2万
查看次数

通过在Haskell中将WHNF转换为NF而感到困惑

在一个简单的例子中,通过打印将WHNF转换为NF可以正常工作

Prelude> let x = 1 + 2 :: Int
Prelude> :sprint x
x = _
Prelude> x
3
Prelude> :sprint x
x = 3
Run Code Online (Sandbox Code Playgroud)

但在某种情况下,类型未声明它不起作用.

Prelude> let x = 1 + 2
Prelude> :sprint x
x = _
Prelude> x
3
Prelude> :sprint x
x = _
Run Code Online (Sandbox Code Playgroud)

你能解释为什么转换在最后一种情况下不起作用吗?

haskell weak-head-normal-form

7
推荐指数
1
解决办法
88
查看次数

无法在生成的线程中调用函数,因为它"无法满足所需的生命周期"

我可以运行这段代码

fn testf(host: &str) {}

fn start(host: &str) {
    testf(host);
    testf(host);
}
Run Code Online (Sandbox Code Playgroud)

但出于某种原因,我无法运行这个:

fn testf(host: &str) {}

fn start(host: &str) {
    thread::spawn(move || testf(host));
    thread::spawn(move || testf(host));
}
Run Code Online (Sandbox Code Playgroud)

因为以下错误

src/server.rs:30:5: 30:18 error: the type `[closure@src/server.rs:30:19: 30:38 host:&str]` does not fulfill the required lifetime
src/server.rs:30     thread::spawn(move || testf(host));
                     ^~~~~~~~~~~~~
note: type must outlive the static lifetime
error: aborting due to previous error
Run Code Online (Sandbox Code Playgroud)

有人可以解释一下,它有什么问题以及如何解决它?

multithreading closures lifetime rust

1
推荐指数
1
解决办法
1350
查看次数