新聞中心
Python作為世界上***的 膠水 語(yǔ)言(哼,世界上***的語(yǔ)言當(dāng)然是PHP==),利用Python的簡(jiǎn)潔和C++的高效,基本可以解決99%的問(wèn)題了吧~

在克州等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強(qiáng)發(fā)展的系統(tǒng)性、市場(chǎng)前瞻性、產(chǎn)品創(chuàng)新能力,以專(zhuān)注、極致的服務(wù)理念,為客戶提供成都網(wǎng)站建設(shè)、做網(wǎng)站 網(wǎng)站設(shè)計(jì)制作定制開(kāi)發(fā),公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),成都品牌網(wǎng)站建設(shè),全網(wǎng)整合營(yíng)銷(xiāo)推廣,成都外貿(mào)網(wǎng)站建設(shè)公司,克州網(wǎng)站建設(shè)費(fèi)用合理。
一般的,Python和C++的交互分為這兩種情況:
- 用C++擴(kuò)展Python:當(dāng)一個(gè)Python項(xiàng)目中出現(xiàn)了性能瓶頸時(shí),將瓶頸部分抽離出來(lái),用C++封裝成一個(gè)Python可以調(diào)用的模塊(so庫(kù));
- 將Python內(nèi)嵌入C++:當(dāng)一個(gè)C++項(xiàng)目中有部分功能預(yù)期將會(huì)經(jīng)常變更需求,期望獲得更高的靈活性時(shí),將這部分功能用Python實(shí)現(xiàn),在C++中進(jìn)行調(diào)用。這篇文章將簡(jiǎn)單介紹下***部分的一種做法。
Boost.Python
Boost作為一個(gè)大寶庫(kù),提供了我們所需要的這一功能。并且,在Boost的許多庫(kù)中,已經(jīng)默認(rèn)使用了Boost.Python,所以也算是經(jīng)過(guò)了充分的測(cè)試。
安裝
Boost的大部分功能都是以頭文件的形式提供的,無(wú)需安裝;但是也有少部分功能,需要進(jìn)行手動(dòng)編譯。不幸,Boost.Python也是其中之一。
參照 Getting Started on Unix Variants 的第五部分內(nèi)容,即可安裝Boost.Python。安裝完成后,可以在相關(guān)目錄(我的是/usr/local/lib下)看到相關(guān)的so文件。
Hello World
用C++實(shí)現(xiàn)一個(gè)模塊,在Python中調(diào)用時(shí),可以返回一個(gè)特定的字符串。
++
- #include
- char const* greet()
- {
- return "hello, boost";
- }
- BOOST_PYTHON_MODULE(hello_boostpy)
- {
- using namespace boost::python;
- def("greet", greet);
- }
太簡(jiǎn)單了,代碼基本說(shuō)明了一切~
將其編譯成動(dòng)態(tài)鏈接庫(kù)的形式:
- g++ -I /usr/include/python2.7/ -fPIC -shared -o hello_boostpy.so hello_boostpy.cc -lboost_python
這時(shí)可以使用ldd看看hello_boostpy.so可不可以找到libboost_python,找不到的話,需要手動(dòng)將其路徑加入環(huán)境變量LD_LIBRARY_PATH中,或者用ldconfig相關(guān)的命令也可以。
接下來(lái)就可以在Python中使用hello_boostpy庫(kù)了:
- # -*- coding: utf-8 -*-
- import sys
- sys.path.append('.')
- def test():
- import hello_boostpy
- return hello_boostpy.greet()
- if __name__ == "__main__":
- print test()
Expose Class
接下來(lái),我們?cè)贑++實(shí)現(xiàn)的模塊中,添加一個(gè)類(lèi),并且嘗試向C++方向傳入Python的list類(lèi)型對(duì)象。
C++類(lèi):
++
- #include
- #include
- #include
- #include
- using namespace boost::python;
- struct Person
- {
- void set_name(std::string name) { this->name = name; }
- std::string print_info();
- void set_items(list& prices, list& discounts);
- std::string name;
- std::vector
item_prices; - std::vector
item_discounts; - };
其中,Python方的list類(lèi)型,在Boost.Python中有一個(gè)對(duì)應(yīng)的實(shí)現(xiàn)boost::python::list(相應(yīng)的,dict、tuple等類(lèi)型都有對(duì)應(yīng)實(shí)現(xiàn))。在set_items中,我們將會(huì)用boost::python::extract對(duì)數(shù)據(jù)類(lèi)型做一個(gè)轉(zhuǎn)換。
++
- void Person::set_items(list& prices, list& discounts)
- {
- for(int i = 0; i < len(prices); ++i)
- {
- double price = extract
(prices[i]); - double discount = extract
(discounts[i]); - item_prices.push_back(price);
- item_discounts.push_back(discount);
- }
- }
Python模塊定義部分依舊是非常直觀的代碼:
- BOOST_PYTHON_MODULE(person)
- {
- class_
("Person") - .def("set_name", &Person::set_name)
- .def("print_info", &Person::print_info)
- .def("set_items", &Person::set_items)
- ;
- }
在Python代碼中,就可以像使用一個(gè)Python定義的類(lèi)一樣使用Person類(lèi)了:
- # -*- coding: utf-8 -*-
- import sys
- sys.path.append('.')
- def test():
- import person
- p = person.Person()
- p.set_name('Qie')
- p.set_items([100, 123.456, 888.8], [0.3, 0.1, 0.5])
- print p.print_info()
- if __name__ == "__main__":
- test()
Py++
上面的模塊封裝過(guò)程,看上去還是有些枯燥,有不少地方都是重復(fù)的工作。那么可不可以自動(dòng)的進(jìn)行呢?Py++提供了這樣的能力,它可以幫你自動(dòng)生成Boost.Python的相關(guān)代碼,對(duì)于接口數(shù)量比較多的模塊來(lái)說(shuō),可以極大的減少工作量,也減少了出錯(cuò)的概率。具體使用方法,可以參見(jiàn) Tutorial
網(wǎng)頁(yè)名稱:打通Python和C++之后?你懂的!
URL鏈接:http://www.5511xx.com/article/cogssdd.html


咨詢
建站咨詢
