当前位置:首页 » 编程语言 » 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()

看注释。


如果解决了您的问题请采纳!
如果未解决请继续追问

热点内容
Ftp打开文件是只读模式 发布:2025-02-09 07:40:55 浏览:504
androidlistview点击事件 发布:2025-02-09 07:25:52 浏览:171
targz解压缩 发布:2025-02-09 06:59:19 浏览:311
wpsphp 发布:2025-02-09 06:58:41 浏览:961
视易锋云系统如何架设辅助服务器 发布:2025-02-09 06:47:08 浏览:770
mysql备份脚本shell 发布:2025-02-09 06:46:33 浏览:15
腾讯云服务器怎样调整分辨率 发布:2025-02-09 06:46:30 浏览:369
php上一个页面 发布:2025-02-09 06:41:25 浏览:489
改装配置后不想重启怎么办 发布:2025-02-09 06:36:40 浏览:446
算法复杂度定义 发布:2025-02-09 06:30:46 浏览:587