C + + extended and embedded Python

Source: Internet
Author: User
Tags in python

Introduction to Python

Python is a simple and powerful interpretive programming language with concise syntax, high efficiency, high level data structure, simple and effective object-oriented programming, especially for fast application development, and can be used to develop large-scale and important commercial applications. Python is an ideal scripting language.

Python free open source, portable to a variety of operating systems, as long as the avoidance of the use of a specific operating system features, Python programs can be run on a variety of platforms without modification.

Python has all the power of modern programming languages, and the Python standard library is huge, helping developers with a variety of tasks such as graphical user interface, file processing, multimedia, regular expressions, document generation, unit testing, threading, database, network communications, Web browsers, CGI, FTP, e-mail, XML, HTML, WAV files, password systems, TK, and other system-related operations. As long as you have Python installed, these features are available in addition to the standard library, there are many other high-quality libraries, such as Wxpython, twisted and Python graphics library, and so on countless.

Python is easy to expand and embed. Many of the standard modules provided by Python support C or C + + interfaces. Python and C can work together, it can be embedded in C or C + + applications, so the use of Python language for the application of the scripting interface, because of support for cross-language development, can be designed to conceptualize the Python application, and gradually migrated to C, before using C rewrite application. (Jython enables Python to work with Java, enabling developers to tune Java packages in Python, or to use Python objects in Java.) Even better, because Jython's interpreter is written entirely in Java, Python programs can be deployed on any platform that supports Java, and even web browsers can run Python scripts directly. )

Ask a question

In a C + + application, we use a set of plug-ins to implement features that have a unified interface, instead of using Python as a plug-in in the form of a dynamic link library, we can easily rewrite the script code based on changes in requirements, rather than having to recompile the link binary dynamic link library. Python is powerful enough, but there are some operating system-specific features that need to be implemented in C + + and then called by Python. So, most basic, we need to do:

1. Embed python in C + + applications, call Python functions in C + + programs, and get values for variables;

2. Use C + + for Python to write an extension module (Dynamic link library), in the Python program to invoke C + + development of extended function functions.

Introduction to Common PYTHON/C APIs

The following is a brief introduction and sample code for several PYTHON/C APIs used in the example. Note that this is not a detailed description of these functions, but only a brief introduction to the features we use, please refer to the documentation [1], [2], [3], [4] for more details.

Open Microsoft Visual Studio. NET 2003, create a new console program, #include <python.h>, and add the sample code to the main function.

//先定义一些变量
char *cstr;
PyObject *pstr, *pmod, *pdict;
PyObject *pfunc, *pargs;
1. void Py_Initialize( )

To initialize the Python interpreter, you must call this function before you use the other PYTHON/C APIs in your C + + program, and a fatal error will occur if the call fails. Cases:

Py_initialize ();

2. int pyrun_simplestring (const char *command)

Execute a piece of Python code as if it were executing in the __MAIN__ function. Cases:

PyRun_SimpleString("from time import time,ctime\n"
"print ''Today is'',ctime(time())\n");

3. pyobject* Pyimport_importmodule (char *name)

Import a python module, parameter name can be the file name of the *.py file. Equivalent to the Python built-in function __import__ (). Cases:

Pmod = Pyimport_importmodule ("Mymod"); mymod.py

4. pyobject* pymodule_getdict (Pyobject *module)

Equivalent to the __dict__ property of the Python module object, the Dictionary object is obtained under the module namespace. Cases:

Pdict = Pymodule_getdict (pmod);

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.