内容简介:python(函数指针和类函数指针)
【 声明:版权所有,欢迎转载,请勿用于商业用途。 联系信箱:feixiaoxing @163.com】
函数指针和类函数指针在 c语言 下面的概念都比较简单,用在脚本下面也很方便。因为脚本语言一切类型都是对象,所以根本不存在指针的概念。一般,我们都是这么用的,
feixiaoxingdeMacBook-Pro-4:~ feixiaoxing$ python Python 2.7.13 (default, Dec 18 2016, 07:03:34) [GCC 4.2.1 Compatible Apple LLVM 7.0.2 (clang-700.1.81)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> def process(f): ... f(1,2,3) ... >>> def add(a,b,c): ... print a,b,c ... >>> process(add) 1 2 3 >>>
这种方法用在一般的函数上没有什么问题。但是如果用在了类函数上面,那么就有点麻烦了,
feixiaoxingdeMacBook-Pro-4:~ feixiaoxing$ python Python 2.7.13 (default, Dec 18 2016, 07:03:34) [GCC 4.2.1 Compatible Apple LLVM 7.0.2 (clang-700.1.81)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> def process(f): ... f(1,2,3) ... >>> class A: ... def run(self,a,b,c): ... print a,b,c ... >>> process(A.run) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<stdin>", line 2, in process TypeError: unbound method run() must be called with A instance as first argument (got int instance instead) >>>
通过系统给出的告警信息,其实 python 已经提示我们要怎么做了,关键是定义一个class实例,用这个实例加上函数作为回调函数就可以了。
feixiaoxingdeMacBook-Pro-4:~ feixiaoxing$ python Python 2.7.13 (default, Dec 18 2016, 07:03:34) [GCC 4.2.1 Compatible Apple LLVM 7.0.2 (clang-700.1.81)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> def process(f): ... f(1,2,3) ... >>> class A: ... def run(self,a,b,c): ... print a,b,c ... >>> val=A() >>> process(val.run) 1 2 3 >>>
通过代码可以看出,类函数并不复杂。只要将类实例和类函数本身联系在一起,就可以作为普通函数的参数进行使用了。
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:- 将数组和矩阵传递给函数,作为C中指针的指针和指针
- C/C++ 函数指针
- flag 中的 Go 函数指针
- CGO 中传递回调函数和指针
- GOLAMG-5 type 不同于C/C++ typedef,切片底层是指针域的函数指针
- Java 8函数式编程模式:不要使用空指针null
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
大数据供应链
娜达·R·桑德斯 (Nada R. Sanders) / 丁晓松 / 中国人民大学出版社 / 2015-7-1 / CNY 55.00
第一本大数据供应链落地之道的权威著作,全球顶级供应链管理专家娜达·桑德斯博士聚焦传统供应链模式向大数据转型,助力工业4.0时代智能供应链构建。 在靠大数据驱动供应链处于领先地位的企业中,45% 是零售商,如沃尔玛、亚马逊,而22%是快消企业,如戴尔电脑。他们都前所未有地掌控了自己的供应链。在库存管理、订单履行率、原材料和产品交付上具有更为广阔的视野。利用具有预见性的大数据分析结果,可以使供需......一起来看看 《大数据供应链》 这本书的介绍吧!