C ++ calls Python (4)

Source: Internet
Author: User
In fact, there are two methods for C ++ to call Python: Find Python modules, classes, methods, and constructor parameters. The second method is to construct a Python script and run it using the python engine. The first method may be more elegant and meets the characteristics of most reflection calls. (In a previous project, I implemented the reflection mechanism of C #. C # Calls COM + and C # Calls JavaScript scripts ).
Another problem is that when two languages call each other, they need to convert data structures (such as basic types, strings, integers, and custom classes, an object in the shared memory. For example, how to import a C ++ object instance to Python and use it in Python. C ++ and Python are not in one process, so they can be implemented using shared_ptr of boost.
The following example demonstrates that C ++ can generate a Python script in C ++ and then use pyrun_simplestring to call python, construct a C ++ object, pass it into python, and call its function in the Python script.
  1. # Include <boost/python. HPP>
  2. # Include <iostream>
  3. Using namespace boost: Python;
  4. Class world
  5. {
  6. Public:
  7. Void set (STD: String MSG) {This-> MSG = MSG ;}
  8. STD: String greet () {return MSG ;}
  9. STD: String MSG;
  10. };
  11. Typedef boost: shared_ptr <world> world_ptr;
  12. Boost_python_module (Hello)
  13. {
  14. Class _ <world> ("world ")
  15. . Def ("Greet", & World: Greet)
  16. . Def ("set", & World: Set)
  17. ;
  18. Register_ptr_to_python <world_ptr> ();
  19. }
  20. Int main (INT argc, char * argv [])
  21. {
  22. Py_initialize ();
  23. World_ptr worldobjectptr (New World );
  24. Worldobjectptr-> set ("hello from C ++! ");
  25. Try
  26. {
  27. Inithello ();
  28. Pyrun_simplestring ("Import hello ");
  29. Object Module (handle <> (borrowed (pyimport_addmodule ("_ main __"))));
  30. Object dictionary = module. ATTR ("_ dict __");
  31. Dictionary ["pyworldobjectptr"] = worldobjectptr;
  32. Pyrun_simplestring ("pyworldobjectptr. Set ('Hello from python! ')");
  33. }
  34. Catch (error_already_set)
  35. {
  36. Pyerr_print ();
  37. }
  38. STD: cout <"worldobjectptr-> greet ():" <worldobjectptr-> greet () <STD: Endl;
  39. Py_finalize ();
  40. Return 0;
  41. }
  42.  

Series of articles:
Python calls C/C ++ functions (1)
Python calls C ++ (2) encapsulated by boost Python)
C ++ calls Python (3)
C ++ calls Python (4)
C ++ and Python advanced application interoperability (5)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.