有一个这样的写法
class Test:
def test_func(self):
pass
def __init__(self):
self.test = Test.test_func
if __name__ == "__main__":
t = Test()
t.test_func() # error
t.test()
Pycharm 已经傻掉了, 对于这段代码不会有任何错误提示,不过它运行起来是错误的
t.test()
TypeError: test_func() missing 1 required positional argument: 'self'
是不是意味着解释器只对对象所属类中的方法自动传递了一个 self 参数,而属于对象实例的方法并没有