簡體   English   中英

pybind11:從 c/c++ 獲取 arguments 到 python function 的編號

[英]pybind11: from c/c++ get number of arguments to python function

在 pybind11 中,我有一個pybind11::function類型的變量。 有什么方法可以確定 c++ 中 function 需要多少 arguments? 即,如果它來自def f(a, b) ,答案將是 2。我意識到這可能會變得瘋狂 w/r *arks、kwargs、self 等......

需要明確的是,這是在 c++ 內部,所以我正在尋找 c++ 代碼。

以下是如何使用inspect.signature()

from inspect import signature

import os # I only imported os to demonstrate with one of its functions

print(signature(os.remove)) # Print out the arguments for the remove fumction from the os module

Output:

(path, *, dir_fd=None)

所以這里是如何在 pybind11 中做到這一點:

#include <pybind11/pybind11.h>
#include <pybind11/embed.h>
pybind11::function callback_; // from somewhere!
pybind11::module inspect_module = pybind11::module::import("inspect");
pybind11::object result = inspect_module.attr("signature")(callback_).attr("parameters");
auto num_params = pybind11::len(result);
// num_params is an int

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM