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