新聞中心
python 類中的變量傳遞給類中的函數(shù)
為了把類中的變量傳遞給類中的函數(shù),我們需要用到3個(gè)特定格式

成都創(chuàng)新互聯(lián)公司長(zhǎng)期為上千客戶提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊(duì)從業(yè)經(jīng)驗(yàn)10年,關(guān)注不同地域、不同群體,并針對(duì)不同對(duì)象提供差異化的產(chǎn)品和服務(wù);打造開(kāi)放共贏平臺(tái),與合作伙伴共同營(yíng)造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為涼山州企業(yè)提供專業(yè)的成都做網(wǎng)站、網(wǎng)站設(shè)計(jì),涼山州網(wǎng)站改版等技術(shù)服務(wù)。擁有10多年豐富建站經(jīng)驗(yàn)和眾多成功案例,為您定制開(kāi)發(fā)。
① 第一個(gè)格式 @classmethod 的中文意思就是“類方法”,@classmethod聲明了函數(shù)1是類方法,這樣才能允許函數(shù)1使用類屬性中的數(shù)據(jù)。
② 第二個(gè)格式 cls 的意思是class的縮寫(xiě)。如果類方法函數(shù)1想使用類屬性(也就是類中的變量),就要寫(xiě)上cls為函數(shù)1的第一個(gè)參數(shù),也就是把這個(gè)類作為參數(shù)傳給自己,這樣就能被允許使用類中的數(shù)據(jù)。
③ 第三個(gè)格式是 cls.變量 。類方法想使用類屬性的時(shí)候,需要在這些變量名稱前加上cls. 這就好比類方法和類之間的約法三章,所以但凡有任何格式錯(cuò)誤都會(huì)報(bào)錯(cuò)。
如果缺①,即缺了“@classmethod”,類方法就不能直接利用類中的屬性,于是報(bào)錯(cuò)
核心解密Python函數(shù)在(類與函數(shù)之間)和(類與類之間)互相調(diào)用
首先來(lái)看一個(gè)函數(shù)間的調(diào)用
類方法:
執(zhí)行結(jié)果:
metaclass能有什么用處,先來(lái)個(gè)感性的認(rèn)識(shí):
1.1 在wiki上面,metaclass是這樣定義的:In object-oriented programming,
a metaclass is a class whose instances are classes.
Just as an ordinary class defines the behavior of certain objects,
a metaclass defines the behavior of certain classes and their instances.
也就是說(shuō)metaclass的實(shí)例化結(jié)果是類,而class實(shí)例化的結(jié)果是instance。我是這么理解的:
metaclass是類似創(chuàng)建類的模板,所有的類都是通過(guò)他來(lái)create的(調(diào)用 new ),這使得你可以自由的控制
創(chuàng)建類的那個(gè)過(guò)程,實(shí)現(xiàn)你所需要的功能。
當(dāng)然你也可以用函數(shù)的方式(下文會(huì)講)
4.1 用類的形式
4.1.1 類繼承于type, 例如: class Meta(type):pass
4.1.2 將需要使用metaclass來(lái)構(gòu)建class的類的 metaclass 屬性(不需要顯示聲明,直接有的了)賦值為Meta(繼承于type的類)
4.2 用函數(shù)的形式
4.2.1 構(gòu)建一個(gè)函數(shù),例如叫metaclass_new, 需要3個(gè)參數(shù):name, bases, attrs,
name: 類的名字
bases: 基類,通常是tuple類型
attrs: dict類型,就是類的屬性或者函數(shù)
4.2.2 將需要使用metaclass來(lái)構(gòu)建class的類的 metaclass 屬性(不需要顯示聲明,直接有的了)賦值為函數(shù)metaclas_new
5.1 basic
metaclass的原理其實(shí)是這樣的:當(dāng)定義好類之后,創(chuàng)建類的時(shí)候其實(shí)是調(diào)用了type的 new 方法為這個(gè)類分配內(nèi)存空間,創(chuàng)建
好了之后再調(diào)用type的 init 方法初始化(做一些賦值等)。所以metaclass的所有magic其實(shí)就在于這個(gè) new 方法里面了。
說(shuō)說(shuō)這個(gè)方法: new (cls, name, bases, attrs)
cls: 將要?jiǎng)?chuàng)建的類,類似與self,但是self指向的是instance,而這里cls指向的是class
name: 類的名字,也就是我們通常用類名. name 獲取的。
bases: 基類
attrs: 屬性的dict。dict的內(nèi)容可以是變量(類屬性),也可以是函數(shù)(類方法)。
所以在創(chuàng)建類的過(guò)程,我們可以在這個(gè)函數(shù)里面修改name,bases,attrs的值來(lái)自由的達(dá)到我們的功能。這里常用的配合方法是
getattr和setattr(just an advice)
下面實(shí)現(xiàn)python中在一個(gè)類中調(diào)用另一個(gè)類的函數(shù)方法
或者下面來(lái)一個(gè)號(hào)理解的例子
執(zhí)行結(jié)果:
先來(lái)介紹內(nèi)部類與外部類是什么?
看源碼解析:
內(nèi)部類調(diào)用外部類的類屬性和類方法
參考文獻(xiàn)1
參考文獻(xiàn)2
參考文獻(xiàn)3
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不用寫(xiě)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類中如何自動(dòng)調(diào)用函數(shù)?
答: 你講的這個(gè)是pyqt里面的內(nèi)容,剛好我最近也一直在學(xué),在代碼當(dāng)中確實(shí)沒(méi)有顯式調(diào)用這個(gè)函數(shù),但是你要知道, keyPressEvent是一個(gè)槽函數(shù),在系統(tǒng)內(nèi)部定義了這個(gè)函數(shù),但是里面沒(méi)有任何代碼,而你就是對(duì)它重寫(xiě)了,就是說(shuō),只要你的鍵盤(pán)里面的任何一個(gè)鍵按下,就相當(dāng)于會(huì)發(fā)送一個(gè)信號(hào),那么在內(nèi)部就會(huì)自動(dòng)調(diào)用這個(gè)函數(shù)。類似的函數(shù)還有 keyReleaseEvent(鍵位松開(kāi)時(shí)自動(dòng)觸發(fā))等。希望可以幫助到你。
相關(guān)介紹
Python3中的super()函數(shù)
super()函數(shù)的用處是調(diào)用當(dāng)前類的父類函數(shù)。在要調(diào)用父類的函數(shù)之外,還需要加一點(diǎn)別的操作的時(shí)候,特別有用。
例:
結(jié)果是:
上面是單繼承的例子,用super()而不是直接用父類的名字去調(diào)用父類函數(shù)的好處是不用管父類的名字。即使父類改名了,super()的調(diào)用依然有效。
多重繼承的時(shí)候需要根據(jù)MRO來(lái)決定調(diào)用順序。詳見(jiàn)官方文檔:
python的類中怎么實(shí)現(xiàn)動(dòng)態(tài)化函數(shù)?
給你這樣一個(gè)例子吧,這個(gè)例子里面有動(dòng)態(tài)增加類的函數(shù)。
聲明一個(gè)類,類初始化的時(shí)候讀取配置文件,根據(jù)配置列表加載特定目錄下的模塊下的函數(shù),函數(shù)和模塊同名,將此函數(shù)動(dòng)態(tài)加載為類的成員函數(shù)。
代碼如下所示:
class WinBAS(Bas):
def __init__(self):
self.__baslist = {}
self.__Init_Modules()
pass
def __Init_Modules(self):
import modplugs
for m in modplugs.__moduleset__:
mh = __import__('modules.' + m)# + '.' + m)
ma = getattr(mh, m)# + '.' + m)
ma = getattr(ma, m)
setattr(self.__class__, m, ma)
modplugs.py是模塊配置文件如下:
__moduleset__ = [
'BAS_GetUserList',
]
然后建立目錄modules下面建立一個(gè)空的__init__.py文件,把目錄變?yōu)橐粋€(gè)包,在modules目錄下建立真正的BAS_GetUserList實(shí)現(xiàn):BAS_GetUserList文件中有個(gè)BAS_GetUserList函數(shù)如下:
def BAS_GetUserList(self, strs):
return [0, strs]
這樣WinBAS類就可以動(dòng)態(tài)加入了BAS_GetUserList函數(shù)。
標(biāo)題名稱:python類強(qiáng)加函數(shù) python 加法函數(shù)
文章出自:http://fisionsoft.com.cn/article/hjgehd.html


咨詢
建站咨詢
