为了账号安全,请及时绑定邮箱和手机立即绑定

请问写两遍instance有意义吗?

请问写两遍instance有意义吗?

撒科打诨 2023-03-18 13:09:37
本来是instance. methodname=types.MethodType(methodName,instance)我改成 methodname=types.MethodType(methodName,instance),即把开头的instance. 去掉,不也能实现一样的功能吗,开头的instance.是不是多余了啊,因为本来就是就是给参数2的instance绑定方法的,写两遍instance有意义吗
查看完整描述

1 回答

?
慕少森

TA贡献2019条经验 获得超9个赞

在Python中,对这两个东西有明确的规定:
函数function —— A series of statements which returns some value to a caller. It can also be passed zero or more arguments which may be used in the execution of the body.

方法method —— A function which is defined inside a class body. If called as an attribute of an instance of that class, the method will get the instance object as its first argument (which is usually called self).
从定义的角度上看,我们知道函数(function)就相当于一个数学公式,它理论上不与其它东西关系,它只需要相关的参数就可以。所以普通的在module中定义的称谓函数是很有道理的。
那么方法的意思就很明确了,它是与某个对象相互关联的,也就是说它的实现与某个对象有关联关系。这就是方法。虽然它的定义方式和函数是一样的。也就是说,在Class定义的函数就是方法。

查看完整回答
反对 回复 2023-03-21
  • 1 回答
  • 0 关注
  • 59 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号