函数与办法的差别
跟着我们越来越频繁应用Python, 我们不免会接触到类, 接触到类属性和办法.然则很多新手包含我, 不知道办法 和 函数 的差别,此次简单来评论辩论下, 如不雅有哪里熟悉不精确, 欲望大年夜神提点指教!
先来看两个定义吧:
function(函数) —— A series of statements which returns some value toa caller. It can also be passed zero or more arguments which may beused in the execution of the body.
method(办法) —— A function which is defined inside a class body. Ifcalled as an attribute of an instance of that class, the methodwill get the instance object as its first argument (which isusually called self).
dis输出解释: 第一列是代码的函数, 第二列是指令的偏移量, 第三列是可视化指令, 第四列是参数, 第五列是指令根据参数计算或者查找的结不雅
大年夜膳绫擎可以看出, 其余编程说话一样, Function也是包含一个函数头和一个函数体, 也同样支撑0到n个形参,而Method则是在function的基本上, 多了一层类的关系, 正因为这一层类, 所以区分了function 和 method.而这个过程是经由过程 PyMethod_New实现的
- PyObject *
- PyMethod_New(PyObject *func, PyObject *self, PyObject *klass)
- {
- register PyMethodObject *im; // 定义办法构造体
- im = free_list;
- if (im != NULL) {
- free_list = (PyMethodObject *)(im->im_self);
- PyObject_INIT(im, &PyMethod_Type); // 初始化
- numfree--;
- }
- else {
- im = PyObject_GC_New(PyMethodObject, &PyMethod_Type);
- if (im == NULL)
- return NULL;
- }
- im->im_weakreflist = NULL;
- Py_INCREF(func);
- /* 往下开端经由过程 func 设备 method*/
- im->im_func = func;
- Py_XINCREF(self);
- im->im_self = self;
- Py_XINCREF(klass);
- im->im_class = klass;
- _PyObject_GC_TRACK(im);
- return
推荐阅读
5月11日下昼,以“视”创将来为主题的首届家庭及时视频家当联盟论坛在深圳福田隆重召开。华为云核心>>>详细阅读
本文标题:Python中的函数与方法 以及Bound Method和Unbound Method
地址:http://www.17bianji.com/lsqh/35218.html
1/2 1

网友点评
精彩导读
科技快报
品牌展示