当前位置:首页 » 编程语言 » python成员函数调用

python成员函数调用

发布时间: 2023-08-14 16:01:43

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文件:

热点内容
java遍历二维数组 发布:2025-03-18 03:36:01 浏览:410
锐捷源码 发布:2025-03-18 03:26:55 浏览:436
诉讼中止裁定后可否解压 发布:2025-03-18 03:24:51 浏览:128
sqlserver全文搜索 发布:2025-03-18 03:23:58 浏览:715
u盘里面文件夹没有了 发布:2025-03-18 03:22:19 浏览:229
华为p系列手机哪个配置好 发布:2025-03-18 03:20:13 浏览:621
易语言连接access数据库 发布:2025-03-18 03:12:48 浏览:661
苗木源码 发布:2025-03-18 03:12:38 浏览:747
oracle卸载数据库 发布:2025-03-18 03:05:15 浏览:46
编译时生成固件怎么办 发布:2025-03-18 03:04:30 浏览:707