如何使用 PyO3 在嵌入 Rust 代码的 Python 代码中执行楼层除法?
How to execute floor division in Python code embedded in Rust code with PyO3?
我正在尝试在 Rust 中内联 Python 代码,但是当 Python 代码具有 floor division //
的运算符时它会失败,就像它是一个一样被忽略铁锈评论。
例如:
#![feature(proc_macro_hygiene)]
use inline_python::python;
fn main() {
python! {
print("Hi from PyO3")
foo = 37.46 // 3
print(foo)
}
}
这将打印 37.46
,即使它应该打印 12.0
(37.46 除以 3 的结果)。
一个可能的解决方案是用 foo = math.floor(37.43 / 3)
替换此除法,但如果可能,我宁愿不必修改 Python 代码。我也担心它可能会影响性能。
有没有办法在使用 PyO3 嵌入到 Rust 代码中的 Python 代码中使用底除法运算符(或等效运算符)?
The // and //= operators are unusable, as they start a comment.
Workaround: you can write ## instead, which is automatically converted to //.
所以试试
#![feature(proc_macro_hygiene)]
use inline_python::python;
fn main() {
python! {
print("Hi from PyO3")
foo = 37.46 ## 3
print(foo)
}
}
我正在尝试在 Rust 中内联 Python 代码,但是当 Python 代码具有 floor division //
的运算符时它会失败,就像它是一个一样被忽略铁锈评论。
例如:
#![feature(proc_macro_hygiene)]
use inline_python::python;
fn main() {
python! {
print("Hi from PyO3")
foo = 37.46 // 3
print(foo)
}
}
这将打印 37.46
,即使它应该打印 12.0
(37.46 除以 3 的结果)。
一个可能的解决方案是用 foo = math.floor(37.43 / 3)
替换此除法,但如果可能,我宁愿不必修改 Python 代码。我也担心它可能会影响性能。
有没有办法在使用 PyO3 嵌入到 Rust 代码中的 Python 代码中使用底除法运算符(或等效运算符)?
The // and //= operators are unusable, as they start a comment.
Workaround: you can write ## instead, which is automatically converted to //.
所以试试
#![feature(proc_macro_hygiene)]
use inline_python::python;
fn main() {
python! {
print("Hi from PyO3")
foo = 37.46 ## 3
print(foo)
}
}