Home > Backend Development > Python Tutorial > Black Horse Cloud Classroom 8 days of in-depth understanding of Python video materials

Black Horse Cloud Classroom 8 days of in-depth understanding of Python video materials

巴扎黑
Release: 2017-08-25 14:53:19
Original
2339 people have browsed it

Python is an object-oriented, interpreted computer programming language invented by Guido van Rossum in 1989. The first public release was released in 1991. Python is purely free software. The source code and interpreter CPython follow the GPL (GNU General Public License) agreement. Python syntax is concise and clear, and one of its features is the mandatory use of white space as statement indentation. Python has rich and powerful libraries. It is often nicknamed the glue language, which can easily connect various modules made in other languages ​​​​(especially C/C++) together. "Black Horse Cloud Classroom 8-Day In-depth Understanding of Python Video Tutorial" will give you an in-depth understanding of the Python language.

Black Horse Cloud Classroom 8 days of in-depth understanding of Python video materials

Video playback address: http://www.php.cn/course/556.html

The difficulty of this video It lies in the call to C/C++:

1. Python calls the C dynamic link library


## Python calls the C library is relatively simple and is packaged into So, just use python's ctypes call.

(1) C language file: pycall.c

/***gcc -o libpycall.so -shared -fPIC pycall.c*/ #include #include

int foo(int a , int b)
{
printf("you input %d and %d\n", a, b);
return a+b;
}

(2) Gcc compiles and generates the dynamic library libpycall.so: gcc -o libpycall.so -shared -fPIC pycall.c. When using g++ to compile functions or methods in the code that generates C dynamic libraries, you need to use extern "C" for compilation.

(3) Python calls the file of the dynamic library: pycall.py

import ctypes
ll = ctypes.cdll.LoadLibrary
lib = ll("./libpycall.so")
lib.foo(1, 3)
print '***finish***'

(4) Running results:

2. Python calls C++( Class) dynamic link library

needs extern "C" to assist, which means that it can still only call C functions and cannot directly call methods, but it can parse C++ methods. Instead of using extern "C", the built dynamic link library does not have symbol tables for these functions.

(1) C++ class file: pycallclass.cpp

#include

using namespace std;

class TestLib
{
public:
void display();
void display(int a);
};
void TestLib::display() {
cout}

void TestLib::display(int a) {
cout}
extern "C" {
TestLib obj;
void display() {
obj.display();
void display_int() {
obj.display(2 );
} }
}


(2) g++ compiles and generates the dynamic library libpycall.so: g++ -o libpycallclass.so -shared -fPIC pycallclass.cpp.

(3) Python calls the file of the dynamic library: pycallclass.py


import ctypes

so = ctypes.cdll.LoadLibrary

lib = so("./ libpycallclass.so")
print 'display()' lib.display()
print 'display(100)' lib.display_int(100)


The teacher's lectures are relaxed and humorous, and the students Learn without stress. From the simple to the deep, from the deep to the simple, relaxed and cheerful is the overall image of this teacher.

The above is the detailed content of Black Horse Cloud Classroom 8 days of in-depth understanding of Python video materials. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template