
正文
大一python调用函数 python入门之函数调用
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
python如何定义和调用函数
1、函数定义
①使用def关键字定义函数
②
def 函数名(参数1.参数2.参数3...):
"""文档字符串大一python调用函数,docstring,用来说明函数大一python调用函数的作用"""
#函数体
return 表达式
注释大一python调用函数的作用大一python调用函数:说明函数是做什么大一python调用函数的,函数有什么功能。
③遇到冒号要缩进,冒号后面所有的缩进的代码块构成了函数体,描述了函数是做什么的,即函数的功能是什么。Python函数的本质与数学中的函数的本质是一致的。
2、函数调用
①函数必须先定义,才能调用,否则会报错。
②无参数时函数的调用:函数名(),有参数时函数的调用:函数名(参数1.参数2.……)
③不要在定义函数的时候在函数体里面调用本身,否则会出不来,陷入循环调用。
④函数需要调用函数体才会被执行,单纯的只是定义函数是不会被执行的。
⑤Debug工具中Step into进入到调用的函数里,Step Into My Code进入到调用的模块里函数。
相关问答
Q1: python的调用函数怎么用?
注意代码格式
python以缩进为标准 而不是像Java 以分号分隔
函数调用需要 写在main函数内
仔细检查你的代码格式和语法
希望可以帮助你 请采纳 谢谢
Q2: python调用c函数
Python是解释性语言, 底层就是用c实现大一python调用函数的, 所以用python调用C是很容易大一python调用函数的, 下面就总结一下各种调用大一python调用函数的方法, 给出例子, 所有例子都在ubuntu9.10, python2.6下试过
1. Python 调用 C (base)
想在python中调用c函数, 如这儿大一python调用函数的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 调用 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_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 调用 C++ (Boost.Python)
Boost库是非常强大的库, 其中的python库可以用来封装c++被python调用, 功能比较强大, 不但可以封装函数还能封装类, 类成员.
首先在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调用函数你的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.
#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
Q3: python如何调用函数中的数组
python调用函数中的数组的方法:
在函数里面使用global定义一个全局变量,然后将数组赋值给这个变量,调用该函数,带有数组的的这个全局变量就可以直接使用了
示例如下:
执行结果如下:
更多Python知识,请关注:Python自学网!!
Q4: python类中如何自动调用函数?
答: 你讲大一python调用函数的这个是pyqt里面大一python调用函数的内容,刚好大一python调用函数我最近也一直在学,在代码当中确实没有显式调用这个函数,但是你要知道, keyPressEvent是一个槽函数,在系统内部定义大一python调用函数了这个函数,但是里面没有任何代码,而你就是对它重写大一python调用函数了,就是说,只要你的键盘里面的任何一个键按下,就相当于会发送一个信号,那么在内部就会自动调用这个函数。类似的函数还有 keyReleaseEvent(键位松开时自动触发)等。希望可以帮助到你。
相关介绍
大一python调用函数的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于python入门之函数调用、大一python调用函数的信息别忘了在本站进行查找喔。






