當前位置:首頁 » 編程語言 » python調用成員函數

python調用成員函數

發布時間: 2022-06-03 17:33:48

python如何在類外定義成員函數

建議不要定義reload函數,如果定義,建議改下名,因為內奸函數中已有reload函數

② python中在類中將類中的函數名賦值給一個普通函數,對象調用成員函數再輸出作者想要告訴我什麼

意思是python中所有變數包括函數名都是一個對象,可以任意賦值

③ python中類內部的函數可以互相調用嗎

可以調用,比如下面,common_func被多個函數調用。


classMyClass:
def__init__(self):
pass
deffunc1(self):
#dosomething

self.common_func()
deffunc2(self):
#dosomething

self.common_func()

defcommon_func(self):
pass

④ Python基礎作業有四道題不會

5、元組不能直接排序,就先轉成list,然後通過list轉

6、這個最簡單,你確定不會?

9、看下面程序吧

10、這個我沒玩過,哈哈,自己找吧

deftitle5(a):
b=list(a)
b.sort(reverse=True)
returntuple(b)


deftitle6(a,b):
returna+b


'''第九題'''
classPerson:

defsay_hello(self):
print("hello!")


print(title5((1,2,3,4,5)))
print(title6(1,2))
p=Person()
p.say_hello()

⑤ python方法中的屬性,實例可否直接調用

"方法中的屬性"這個說法有點不清楚。。

  1. 類的"屬性"指的是類的成員變數,類的實例可以使用類的屬性。

  2. 類的「方法」指的是類的成員函數;方法既可以調用類的屬性,也可以定義自己的局部變數。方法的局部變數不能被方法以外的任何函數調用。

⑥ Python的成員函數問題

  1. join() 是str類中的一個成員函數,它是類成員。


2. python中視一切為對象,所以a其實是一個字元串對象,具有成員函數join,可以用dir(a)查看。


3. a.join() 就是字元對象a調用其成員函數 join()

⑦ python class成員函數沒有實現

pythonclass成員函數沒有實現是因為:
def是定義函數,就是封裝一段代碼,執行特定功能。class是定義對象,對象有自己的成員變數和成員函數。

⑧ python調用c函數

Python是解釋性語言, 底層就是用c實現的, 所以用python調用C是很容易的, 下面就總結一下各種調用的方法, 給出例子, 所有例子都在ubuntu9.10, python2.6下試過
1. Python 調用 C (base)
想在python中調用c函數, 如這兒的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_InitMole("example", exampleMethods);
}

把這段代碼存為wrapper.c, 編成so庫,
gcc -fPIC wrapper.c -o example.so -shared -I/usr/include/python2.6 -I/usr/lib/python2.6/config

然後在有此so庫的目錄, 進入python, 可以如下使用
import example
example.fact(4)

2. Python 調用 C++ (base)
在python中調用C++類成員函數, 如下調用TestFact類中的fact函數,
#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" //不加會導致找不到initexample
void initexample()
{
PyObject* m;
m = Py_InitMole("example", exampleMethods);
}

把這段代碼存為wrapper.cpp, 編成so庫,
g++ -fPIC wrapper.cpp -o example.so -shared -I/usr/include/python2.6 -I/usr/lib/python2.6/config

然後在有此so庫的目錄, 進入python, 可以如下使用
import example
example.fact(4)

3. Python 調用 C++ (Boost.Python)
Boost庫是非常強大的庫, 其中的python庫可以用來封裝c++被python調用, 功能比較強大, 不但可以封裝函數還能封裝類, 類成員.
http://dev.gameres.com/Program/Abstract/Building%20Hybrid%20Systems%20with%20Boost_Python.CHN.by.JERRY.htm
首先在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庫
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路徑設為你的python路徑, 並且必須加-lboost_python-gcc42-mt-1_34_1, 這個庫名不一定是這個, 去/user/lib查

然後在有此so庫的目錄, 進入python, 可以如下使用
>>> import hello
>>> hello.greet()
'hello, world'

4. python 調用 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.
http://python.net/crew/theller/ctypes/

#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庫,
g++ -fPIC wrapper.cpp -o example.so -shared -I/usr/include/python2.6 -I/usr/lib/python2.6/config

進入python, 可以如下使用
>>> import ctypes
>>> pdll = ctypes.CDLL('/home/ubuntu/tmp/example.so')
>>> pdll.fact(4)
12

⑨ python中如何對類的成員函數開啟線程

#-*-coding:utf-8-*-
importthreading
importthread
importtime


classTest(object):
def__init__(self):
#threading.Thread.__init__(self)
self._sName="machao"

defprocess(self):
#args是關鍵字參數,需要加上名字,寫成args=(self,)
th1=threading.Thread(target=Test.buildList,args=(self,))
th1.start()
th1.join()

defbuildList(self):
whileTrue:
print"start"
time.sleep(3)


test=Test()
test.process()

看注釋。


如果解決了您的問題請採納!
如果未解決請繼續追問

熱點內容
php樹菜單 發布:2025-02-09 10:04:10 瀏覽:359
linux保存ip 發布:2025-02-09 10:04:10 瀏覽:23
四川霜狼伺服器怎麼樣 發布:2025-02-09 10:02:44 瀏覽:145
Vs中h編譯選項是灰的 發布:2025-02-09 10:01:59 瀏覽:556
安卓43怎麼升級44 發布:2025-02-09 09:51:33 瀏覽:463
美國雲伺服器快還是香港快 發布:2025-02-09 09:34:33 瀏覽:988
怎麼解壓qq文件 發布:2025-02-09 09:18:14 瀏覽:581
安卓最新怎麼調靈敏度更穩 發布:2025-02-09 09:12:44 瀏覽:400
豌豆莢如何用安卓手機下載 發布:2025-02-09 09:11:57 瀏覽:213
吃雞腳本輔助 發布:2025-02-09 09:09:29 瀏覽:6