python成員函數調用
❶ python實例成員實例變數
1、語法:
定義:對象.變數名
調用:對象.變數名
2、說明:
(1)首次通過對象賦值為創建,再次賦值為修改
x01=wife()
wife.name="做租橡張三" 此時為創建
wife.name="李四" 此時為修改
(2)通過在構造函數__init__中創建
wife("張三",24)
print(w01.name)
(3)、每個對象存儲一份,通過對象地址訪問。
3、作用:描述所有對象的共有數據。
實例方法:
1、語法
定義:def方法名稱(self,參數列表):
方法體
調用:對象地址.實例名稱(參數)
作用:所有對象共享方法。
對象是指構造函數,方法純旁是指形為函數。
'''
class Studons:
def __init__(self,name,xibie,nianling,fenshu):
self.name=name
self.xibie=xibie
self.nianling=nianling
self.fenshu=fenshu
def dayin(self):
print("姓名:%s,性別:%s,年齡:%d,分數:%d"%(self.name,self.xibie,self.nianling,self.fenshu))
#定義函數根據姓名查找每型拿個人的信息。
list01=[Studons("張無忌","女",2,98),
Studons("張老六","男",61,95),
Studons("三豐","男",31,95),
Studons("小麗麗","女",21,93),
]
def cha_name(name):
for item in list01:
if name==item.name:
# print(item.name,item.nianling)
item.dayin()
cha_name("小麗麗")
#2、
def fund_xus(names):
for itme in list01:
if itme.name==names:
return itme
print(fund_xus("小麗麗").name)
#查找所有女同學
def fund_nv(age):
list02 = []
for itme in list01:
if itme.xibie==age:
list02.append(itme)
return list02
ae=fund_nv("女")
for ime in ae:
ime.dayin()
#統計30歲以上的人的數量
def count_stud():
coun=0
for itme in list01:
if itme.nianling>=30:
coun+=1
return coun
print(count_stud())
#將所有學生的成績歸0
def list_0():
for itme in list01:
itme.fenshu=0
#獲取列表中所有人的名字
def pen_name():
name=[]
for itme in list01:
name.append(itme.name)
return name
print(pen_name())
#查找年齡 最大的學生對象
def max_nl():
maxt=list01[0]
for itme in range(len(list01)):
if maxt.nianling<list01[itme].nianling: span=""> </list01[itme].nianling:>
maxt=list01[itme]
return maxt
max_nl().dayin()
❷ 怎樣讓python多次調用函數.讓函數返回的值相加
下面是一個例子,用 for 循環調用 10 次 test 函數,並把返回值加到 sum 變數中:
#coding=utf-8
deftest(n):
returnn+1
sum=0
#使用for循環調用10次test函數
foriinrange(10):
#調用test函數,並將返回值加到sum中
sum=sum+test(i)
print('sum='+str(sum))
❸ python 特殊方法定製類__radd__ and __iadd__()如何使用的
__radd__是自定義的類操作符,執行「右加」。當python解釋器執行到a+b這樣的語句時,首先在查找a中有沒有__add__操作符,如果a中沒有定義,那麼就在b中查找並執行__radd__。
下面是個簡單例子
class A:
pass
class B:
def __radd__(self, a):
print 'B.__radd__'
return a.v + self.v
class C:
def __add__(self, b):
print 'C.__add__'
return self.v + b.v
a = A()
a.v = 1
b = B()
b.v = 2
c = C()
c.v = 3
print a + b #因為a中沒有__add__,所以調用的是B.__radd__
print c + b #c中有__add__,所以調用的是C.__add__
至於__iadd__(),是運算符類operator的成員函數,就是累加操作符的另一種調用形式。
a = operator.__iadd__(a, b)就等價於a += b
❹ python 如何根據輸入參數調用不同的函數
#Python3.x
deffunc():
c=input("PleaseEnteraChar:")
while(True):
ifc=='a':
func_a()
break;
ifc=='b':
func_b()
break;
func()
❺ python,定義一個函數A,函數B有一個變數,函數調用時怎麼在函數A里獲取變數的值。可以用資料庫是什麼的
你的問題感覺有些困惑,推薦答案里應該是正解。不過後面又有朋友回復你用類來表達陪寬山。我覺著他們都說得對。巧凱你先想清楚,你的數據結構的組織關系。
哪些是需要隱藏的,為什麼要隱藏。不隱藏會有什麼後果。然後你再想用函數+變數方式還是用類方式。
明確說明函數是沒有靜態變數的。所以不存在函數里有一個變數,幾個其它的函數可以引用,這個變數又是封閉隱藏的。這感覺思維蘆中劉有些混亂。
只要你自己想清楚了,邁過這一關,自然就會自己找到答案的。
def fun1(parameter1):
var b
b=333
return b
def fun2(parameter2):
c=fun1(parameter2*3)
return c
不知道你是不是想表達這樣的結果。
❻ 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()
看注釋。
如果解決了您的問題請採納!
如果未解決請繼續追問
❽ python如何調用另一個py文件的所有函數
在同一個文件夾下
調用函數:
A.py文件: