python向c函數(shù)傳參 python 類之間傳值

python怎么傳參

Python函數(shù)參數(shù)傳遞機(jī)制問(wèn)題在本質(zhì)上是調(diào)用函數(shù)(過(guò)程)和被調(diào)用函數(shù)(過(guò)程)在調(diào)用發(fā)生時(shí)進(jìn)行通信的方法問(wèn)題?;镜膮?shù)傳遞

成都創(chuàng)新互聯(lián)專業(yè)為企業(yè)提供新?lián)峋W(wǎng)站建設(shè)、新?lián)嶙鼍W(wǎng)站、新?lián)峋W(wǎng)站設(shè)計(jì)、新?lián)峋W(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁(yè)設(shè)計(jì)與制作、新?lián)崞髽I(yè)網(wǎng)站模板建站服務(wù),10多年新?lián)嶙鼍W(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(wǎng)絡(luò)服務(wù)。

機(jī)制有兩種:值傳遞和引用傳遞。值傳遞(passl-by-value)過(guò)程中,被調(diào)函數(shù)的形式參數(shù)作為被調(diào)函數(shù)的局部變量處理,即在堆棧中開

辟了內(nèi)存空間以存放由主調(diào)函數(shù)放進(jìn)來(lái)的實(shí)參的值,從而成為了實(shí)參的一個(gè)副本。值傳遞的特點(diǎn)是被調(diào)函數(shù)對(duì)形式參數(shù)的任何操作都是作

為局部變量進(jìn)行,不會(huì)影響主調(diào)函數(shù)的實(shí)參變量的值。(推薦學(xué)習(xí):Python視頻教程)

引用傳遞(pass-by-reference)過(guò)程中,被調(diào)函數(shù)的形式參數(shù)雖然也作為局部變量在堆棧中開辟了內(nèi)存空間,但是這時(shí)存放的是由主調(diào)函

數(shù)放進(jìn)來(lái)的實(shí)參變量的地址。被調(diào)函數(shù)對(duì)形參的任何操作都被處理成間接尋址,即通過(guò)堆棧中存放的地址訪問(wèn)主調(diào)函數(shù)中的實(shí)參變量。正

因?yàn)槿绱?,被調(diào)函數(shù)對(duì)形參做的任何操作都影響了主調(diào)函數(shù)中的實(shí)參變量。

Python 如何給 c 函數(shù)傳遞結(jié)構(gòu)體參數(shù)

//test1.c#?include?stdio.h#?include?stdlib.hstruct?Student

{????char?name[30];????float?fScore[3];

};void?Display(struct?Student?su){????printf("-----Information------\n");????printf("Name:%s",su.name);????printf("Chinese:%.2f\n",su.fScore[0]);????printf("Math:%.2f\n",su.fScore[1]);????printf("English:%.2f",su.fScore[2]);????printf("平均分?jǐn)?shù)為:%.2f\n",(su.fScore[0]+su.fScore[1],su.fScore[2])/3);

}

如何將python中的dict作為參數(shù)傳入c函數(shù)中用c做相關(guān)的處理?

#先上代碼再解釋

static?PyObject?*keywdarg_parrot(PyObject?*self,?PyObject?*args,?PyObject?*keywds)

{

int?voltage;

char?*state?=?"a?stiff";

char?*action?=?"voom";

char?*type?=?"Norwegian?Blue";

static?char?*kwlist[]?=?{"voltage",?"state",?"action",?"type",?NULL};

if?(!PyArg_ParseTupleAndKeywords(args,?keywds,?"i|sss",?kwlist,

voltage,?state,?action,?type))

return?NULL;

printf("--?This?parrot?wouldn't?%s?if?you?put?%i?Volts?through?it.\n",

action,?voltage);

printf("--?Lovely?plumage,?the?%s?--?It's?%s!\n",?type,?state);

Py_INCREF(Py_None);

return?Py_None;

}

static?PyMethodDef?keywdarg_methods[]?=?{

/*?The?cast?of?the?function?is?necessary?since?PyCFunction?values

*?only?take?two?PyObject*?parameters,?and?keywdarg_parrot()?takes

*?three.

*/

{"parrot",?(PyCFunction)keywdarg_parrot,?METH_VARARGS?|?METH_KEYWORDS,

"Print?a?lovely?skit?to?standard?output."},

{NULL,?NULL,?0,?NULL}???/*?sentinel?*/

};

PyObject?*?initkeywdarg(void)

{

/*?Create?the?module?and?add?the?functions?*/

return?Py_InitModule("keywdarg",?keywdarg_methods);

}

這是一個(gè)函數(shù)(keywdarg_parrot)即使用了元組參數(shù),也使用了字典參數(shù)的例子。例子出自Python2.7.2的文檔。具體在:

Python v2.7.2 documentation #187;Extending and Embedding the Python

Interpreter

這個(gè)頁(yè)面下。文檔里面有一些關(guān)于C/C++與Python交互的介紹和例子,還是比較詳細(xì)的。傳遞字典參量要注意,

{"parrot",?(PyCFunction)keywdarg_parrot,?METH_VARARGS?|?METH_KEYWORDS,

"Print?a?lovely?skit?to?standard?output."},

這里的METH_VARARGS | METH_KEYWORDS,與普通的不同。解析用:

PyArg_ParseTupleAndKeywords(args,?keywds,?"i|sss",?kwlist,

voltage,?state,?action,?type)

其中的四個(gè)變量要提前聲明好,這里分別是int,str,str,str類型的。int對(duì)應(yīng)的是args接受到的值。string的都是keywds里面的,它們都是有初始值的。kwlist是變量的名字,就是在python里調(diào)用的時(shí)候使用的keyword名稱。照著例子的模式可以改成其它的,能用的。具體是怎么工作的,其實(shí)我也太明白。

Python代碼是這樣的調(diào)用的時(shí)候:

print?keywdarg.parrot(10,"LHJ",'HKJ','ER')

print?keywdarg.parrot(10,"LHJ",'HKJ')

print?keywdarg.parrot(10,"LHJ",type='KJ')

輸出分別是:

-- This parrot wouldn't HKJ if you put 10 Volts through it.

-- Lovely plumage, the ER -- It's LHJ!

None

-- This parrot wouldn't HKJ if you put 10 Volts through it.

-- Lovely plumage, the Norwegian Blue -- It's LHJ!

None

-- This parrot wouldn't voom if you put 10 Volts through it.

-- Lovely plumage, the KJ -- It's LHJ!

None

第二次調(diào)用省略掉了變量,也能正常執(zhí)行。第三次調(diào)用,變量type本來(lái)是第四位的,現(xiàn)在變成了keyword并寫在了第三位,是python代碼里調(diào)用的常見形式:keyword不講順序,省略掉的keyword使用了默認(rèn)值。

就這些了,其它的你再看一下Python的文檔吧。

python函數(shù)調(diào)用的參數(shù)傳遞

python的函數(shù)參數(shù)傳遞是"引用傳遞(地址傳遞)"。

python中賦值語(yǔ)句的過(guò)程(x = 1):先申請(qǐng)一段內(nèi)存分配給一個(gè)整型對(duì)象來(lái)存儲(chǔ)數(shù)據(jù)1,然后讓變量x去指向這個(gè)對(duì)象,實(shí)際上就是指向這段內(nèi)存(這里有點(diǎn)和C語(yǔ)言中的指針類似)。

在Python中,會(huì)為每個(gè)層次生成一個(gè)符號(hào)表,里層能調(diào)用外層中的變量,而外層不能調(diào)用里層中的變量,并且當(dāng)外層和里層有同名變量時(shí),外層變量會(huì)被里層變量屏蔽掉。函數(shù)? 調(diào)用 ?會(huì)為函數(shù)局部變量生成一個(gè)新的符號(hào)表。

局部變量:作用于該函數(shù)內(nèi)部,一旦函數(shù)執(zhí)行完成,該變量就被回收。

全局變量:它是在函數(shù)外部定義的,作用域是整個(gè)文件。全局變量可以直接在函數(shù)里面應(yīng)用,但是如果要在函數(shù)內(nèi)部改變?nèi)肿兞?,必須使用global關(guān)鍵字進(jìn)行聲明。

注意 :默認(rèn)值在函數(shù)? 定義 ?作用域被解析

在定義函數(shù)時(shí),就已經(jīng)執(zhí)行力它的局部變量

python中不可變類型是共享內(nèi)存地址的:把相同的兩個(gè)不可變類型數(shù)據(jù)賦給兩個(gè)不同變量a,b,a,b在內(nèi)存中的地址是一樣的。

python調(diào)用c函數(shù)

Python是解釋性語(yǔ)言, 底層就是用c實(shí)現(xiàn)的, 所以用python調(diào)用C是很容易的, 下面就總結(jié)一下各種調(diào)用的方法, 給出例子, 所有例子都在ubuntu9.10, python2.6下試過(guò)

1. Python 調(diào)用 C (base)

想在python中調(diào)用c函數(shù), 如這兒的fact

#include Python.h

int fact(int n)

{

if (n = 1)

return 1;

else

return n * fact(n - 1);

}

PyObject* wrap_fact(PyObject* self, PyObject* args)

{

int n, result;

if (! PyArg_ParseTuple(args, "i:fact", n))

return NULL;

result = fact(n);

return Py_BuildValue("i", result);

}

static PyMethodDef exampleMethods[] =

{

{"fact", wrap_fact, METH_VARARGS, "Caculate N!"},

{NULL, NULL}

};

void initexample()

{

PyObject* m;

m = Py_InitModule("example", exampleMethods);

}

把這段代碼存為wrapper.c, 編成so庫(kù),

gcc -fPIC wrapper.c -o example.so -shared -I/usr/include/python2.6 -I/usr/lib/python2.6/config

然后在有此so庫(kù)的目錄, 進(jìn)入python, 可以如下使用

import example

example.fact(4)

2. Python 調(diào)用 C++ (base)

在python中調(diào)用C++類成員函數(shù), 如下調(diào)用TestFact類中的fact函數(shù),

#include Python.h

class TestFact{

public:

TestFact(){};

~TestFact(){};

int fact(int n);

};

int TestFact::fact(int n)

{

if (n = 1)

return 1;

else

return n * (n - 1);

}

int fact(int n)

{

TestFact t;

return t.fact(n);

}

PyObject* wrap_fact(PyObject* self, PyObject* args)

{

int n, result;

if (! PyArg_ParseTuple(args, "i:fact", n))

return NULL;

result = fact(n);

return Py_BuildValue("i", result);

}

static PyMethodDef exampleMethods[] =

{

{"fact", wrap_fact, METH_VARARGS, "Caculate N!"},

{NULL, NULL}

};

extern "C" //不加會(huì)導(dǎo)致找不到initexample

void initexample()

{

PyObject* m;

m = Py_InitModule("example", exampleMethods);

}

把這段代碼存為wrapper.cpp, 編成so庫(kù),

g++ -fPIC wrapper.cpp -o example.so -shared -I/usr/include/python2.6 -I/usr/lib/python2.6/config

然后在有此so庫(kù)的目錄, 進(jìn)入python, 可以如下使用

import example

example.fact(4)

3. Python 調(diào)用 C++ (Boost.Python)

Boost庫(kù)是非常強(qiáng)大的庫(kù), 其中的python庫(kù)可以用來(lái)封裝c++被python調(diào)用, 功能比較強(qiáng)大, 不但可以封裝函數(shù)還能封裝類, 類成員.

首先在ubuntu下安裝boost.python, apt-get install libboost-python-dev

#include boost/python.hpp

char const* greet()

{

return "hello, world";

}

BOOST_PYTHON_MODULE(hello)

{

using namespace boost::python;

def("greet", greet);

}

把代碼存為hello.cpp, 編譯成so庫(kù)

g++ hello.cpp -o hello.so -shared -I/usr/include/python2.5 -I/usr/lib/python2.5/config -lboost_python-gcc42-mt-1_34_1

此處python路徑設(shè)為你的python路徑, 并且必須加-lboost_python-gcc42-mt-1_34_1, 這個(gè)庫(kù)名不一定是這個(gè), 去/user/lib查

然后在有此so庫(kù)的目錄, 進(jìn)入python, 可以如下使用

import hello

hello.greet()

'hello, world'

4. python 調(diào)用 c++ (ctypes)

ctypes is an advanced ffi (Foreign Function Interface) package for Python 2.3 and higher. In Python 2.5 it is already included.

ctypes allows to call functions in dlls/shared libraries and has extensive facilities to create, access and manipulate simple and complicated C data types in Python - in other words: wrap libraries in pure Python. It is even possible to implement C callback functions in pure Python.

#include Python.h

class TestFact{

public:

TestFact(){};

~TestFact(){};

int fact(int n);

};

int TestFact::fact(int n)

{

if (n = 1)

return 1;

else

return n * (n - 1);

}

extern "C"

int fact(int n)

{

TestFact t;

return t.fact(n);

}

將代碼存為wrapper.cpp不用寫python接口封裝, 直接編譯成so庫(kù),

g++ -fPIC wrapper.cpp -o example.so -shared -I/usr/include/python2.6 -I/usr/lib/python2.6/config

進(jìn)入python, 可以如下使用

import ctypes

pdll = ctypes.CDLL('/home/ubuntu/tmp/example.so')

pdll.fact(4)

12

本文題目:python向c函數(shù)傳參 python 類之間傳值
當(dāng)前URL:http://muchs.cn/article32/hgcjsc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計(jì)、網(wǎng)站策劃、定制開發(fā)、商城網(wǎng)站、自適應(yīng)網(wǎng)站、品牌網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

營(yíng)銷型網(wǎng)站建設(shè)