Skip to content

return custom struct as python object(which aslo could be none) #3591

Answered by linrongbin16
linrongbin16 asked this question in Q&A
Discussion options

You must be logged in to vote

Turns out it's my mistake. I didn't fetch the correct data element in mysql C connector's result set.

My C++ compiler/runtime(macOS clang-12) didn't throw any exception when I use the pybind11 built module.

And finally, both of these two API works correctly after I fix my bug:

  • UserModel *get_user1(const std::string &user_name) const (create new instance and return to pybind11, return nullptr if no user exists)
  • std::optional<UserModel> get_user2(const std::string &user_name) const (return copy of instance to pybind11, return std::nullopt if no user exists)

Personally, I prefer the second API since it's avoid raw pointer.

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by linrongbin16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant