下面是我的main.rs
文件:
use pyo3::prelude::*;
use pyo3::types::{PyDateTime, PyDate};
fn main() -> () {
Python::with_gil(|py| {
let datetime = py.import("datetime").unwrap();
let my_datetime = datetime.call_method1("datetime", (2022, 3, 28, 0, 0, 0, 0)).unwrap();
println!("isinstance pydatetime: {:?}", my_datetime.is_instance_of::<PyDateTime>().unwrap());
println!("isinstance pydate: {:?}", my_datetime.is_instance_of::<PyDate>().unwrap());
let my_date = datetime.call_method1("date", (2022, 3, 28)).unwrap();
println!("isinstance pydatetime: {:?}", my_date.is_instance_of::<PyDateTime>().unwrap());
println!("isinstance pydate: {:?}", my_date.is_instance_of::<PyDate>().unwrap());
})
}
下面是我的Cargo.toml
文件:
[package]
name = "tmp"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
pyo3 = { version = "0.18.0", features = ["auto-initialize"] }
如果我运行上面的代码,我得到
isinstance pydatetime: true
isinstance pydate: true
isinstance pydatetime: true
isinstance pydate: true
我觉得这有点奇怪,因为直接在Python中运行,我得到:
In [27]: import datetime as dt
In [28]: isinstance(my_datetime, dt.datetime)
Out[28]: True
In [29]: isinstance(my_datetime, dt.date)
Out[29]: True
In [30]: isinstance(my_date, dt.datetime)
Out[30]: False
In [31]: isinstance(my_date, dt.date)
Out[31]: True
所以我本以为
my_date.is_instance_of::<PyDateTime>().unwrap()
也是false
为什么不是这样呢?我如何区分一个对象是datetime
还是date
,而不需要求助于像检查dt.get_type().name()
这样的更简单的方法?
1条答案
按热度按时间xoshrz7s1#
我已经向PyO3提交了一个pull请求,应该可以解决这个问题:https://github.com/PyO3/pyo3/pull/3071