Recommended
20170131 python3 6 PEP526
PyCon2020 Pythonで競プロをしよう! 〜入門者が知っておくべき高速化Tips〜
Pythonの処理系はどのように実装され,どのように動いているのか? 我々はその実態を調査すべくアマゾンへと飛んだ.
Python パッケージの影響を歴史から理解してみよう!
Python 機械学習プログラミング データ分析ライブラリー解説編
Wrapping a C++ library with Cython
Apilecture for 2014/02/22 at shannonlab
Python と型ヒント (Type Hints)
Goroutineと channelから はじめるgo言語
LLdeade Python Language Update
PyCon JP 2014 plone terada
次世代言語 Python による PyPy を使った次世代の処理系開発
Pyconjp2014_implementations
「Python言語」はじめの一歩 / First step of Python / 2016 Jan 12
More Related Content
20170131 python3 6 PEP526
PyCon2020 Pythonで競プロをしよう! 〜入門者が知っておくべき高速化Tips〜
Pythonの処理系はどのように実装され,どのように動いているのか? 我々はその実態を調査すべくアマゾンへと飛んだ.
Python パッケージの影響を歴史から理解してみよう!
What's hot (19)
Python 機械学習プログラミング データ分析ライブラリー解説編
Wrapping a C++ library with Cython
Apilecture for 2014/02/22 at shannonlab
Python と型ヒント (Type Hints)
Goroutineと channelから はじめるgo言語
LLdeade Python Language Update
PyCon JP 2014 plone terada
Similar to Boost.python (20) 次世代言語 Python による PyPy を使った次世代の処理系開発
Pyconjp2014_implementations
「Python言語」はじめの一歩 / First step of Python / 2016 Jan 12
Python languageupdate (2004)
「Python言語」はじめの一歩 / First step of Python
Python & PyConJP 2014 Report
Hello World Python featuring GAE
開発環境構築からはじめるPython VisualStudio Codeとpipenvで始めるpython
Boost.python
25. // sample1.cpp //関数のラッピング#define BOOST_PYTHON_STATIC_LIB // Boost.Pythonライブラリを静的にリンクする#include <string>#include <boost/python.hpp>std::string hello_world(void){ return "Hellow, world!";}BOOST_PYTHON_MODULE(sample1) // sample1はpython拡張Moduleの名前になる{ // C++のhello_world関数を、" hello_world_python"という名前でPythonに公開するboost::python::def ("hello_world_python", &hello_world);} 26. Python 2.7.1 (r271:86832, Nov 27 2010, 18:30:46) [MSC v.1500 32 bit (Intel)] on win32Type "help", "copyright", "credits" or "license" for more information.>>> import sample1>>> dir (sample1)['__doc__', '__file__', '__name__', '__package__', 'hello_world_python']>>> sample1.hello_world_python<Boost.Python.function object at 0x00E43990>>>> sample1.hello_world_python()'Hellow, world!'>>> print sample1.hello_world_python()Hellow, world!>>> 27. // sample2.cpp //クラスのラッピング#define BOOST_PYTHON_STATIC_LIB / / Boost.Pythonライブラリを静的にリンクする #include <boost/python.hpp> structCValue{ intgetValue(void) const { return value; } void setValue(int value) { this->value = value; } int value; }; BOOST_PYTHON_MODULE(sample2) // sample2はpython拡張モジュールの名前になる { // C++のCValueクラス(構造体)を、”PValue”という名前で pythonに公開する boost::python::class_<CValue>("PValue") .def(“get”, &CValue::getValue) ; // getValueメンバ関数を、”get”という名前のメソッド として公開する .def("set", &CValue::setValue); // setValueメンバ関数を、”set”という名前のメソッド として公開する } 28. // sample3.cpp //C++のアクセサをPythonのプロパティに公開#define BOOST_PYTHON_STATIC_LIB // Boost.Pythonライブラリを静的にリンクする #include <boost/python.hpp> structCValue{ intgetValue(void) const { return value; } void setValue(int value) { this->value = value; } int value; }; BOOST_PYTHON_MODULE(sample3) // sample3は python拡張モジュールの名前になる { // C++のCValueクラス(構造体)を、”PValue”という名前で pythonに公開する boost::python::class_<CValue>("PValue") //“value”プロパティは読み書き可 .add_property("value", &CValue::getValue, &CValue::setValue) //“rvalue”プロパティは読み取り専用 .add_property("rvalue", &CValue::getValue); } 31. // sample4.cpp //C++上でPythonインタプリタの実行#define BOOST_PYTHON_STATIC_LIB // Boost.Pythonライブラリを静的にリンクする #include <iostream> #include <boost/python.hpp> void c_plus_plus(void) { std::cout << "C++" << std::endl; } int main() { // Pythonインタプリタを初期化する Py_Initialize(); // "__main__"モジュールをインポートする boost::python::object module = boost::python::import("__main__"); // "__main__"モジュールの名前空間でコードを実行する boost::python::object ns = module.attr("__dict__"); // "cpp"という名前で Pythonのインタプリタから呼び出す ns["cpp"] = &c_plus_plus; // Pythonのインタプリタを実行する boost::python::exec( "print('Hello, World!')\n" "cpp() \n" "value=2**20\n" , ns); // "__main__"モジュールの名前空間で定義した変数の値を取り出す int v = boost::python::extract<int>(ns["value"]); std::cout << v << std::endl; // Pythonインタプリタの終了処理を行う Py_Finalize(); }