>>> class Student(): def choose(self): print('来自类函数') >>> stu1 =Student() >>> stu1.choose() 来自类函数
这个简单能理解!
>>> class Student(): def choose(self): print('来自类函数') >>> stu1 =Student() >>> stu1.choose() 来自类函数 >>> def choose(self): print('来自对象的函数') >>> stu1.choose=choose >>> stu1.__dict__ {'choose': <function choose at 0x01EFBB68>} >>> stu1.choose() Traceback (most recent call last): File "<pyshell#14>", line 1, in <module> stu1.choose() TypeError: choose() missing 1 required positional argument: 'self' >>>
>>> stu1.choose(1)
来自对象的函数
>>>
结论:
类的函数被对象调用可以自动传参,但是对象函数被对象调用就不能传参了