首页 > 其他 > 详细

无参方法只能类调用吗?并不是

时间:2021-07-29 16:02:47      阅读:17      评论:0      收藏:0      [点我收藏+]
class MethodTest:

def inner_test(self):
print(‘in class‘)

def outer_test():
print(‘out of class‘)

mt = MethodTest()
mt.outer_test = outer_test

mt.inner_test() # in class
mt.outer_test() # out of class

无参方法如果想实例化对象调用,无参方法需写在类外头,然后调用的时候格式就是实例化对象.无参方法不加括号 = 无参方法不加括号
因为实例对象只有在引用方法时,才会将自身作为第一个参数传递;调用实例对象的普通函数,则不会。

无参方法只能类调用吗?并不是

原文:https://www.cnblogs.com/jasonsheng/p/15074605.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!