类的函数属性绑定给不同对象,是不同的绑定方法。对象调用绑定方法时会把对象本身当做self参数传入
示例代码
class People: country = ‘China‘ ? def __init__(self, name, sex, age): self.Name = name self.Sex = sex self.Age = age ? def test(self): print(self) ? ? conan = People(‘Conan‘, ‘male‘, 8) wukong = People(‘wukong‘, ‘male‘, 18) ? wukong.test() # People.test(wukong) conan.test() # People.test(conan) ? print(People.test,People.test(123)) print(wukong.test,wukong.test()) print(conan.test,wukong.test())
打印结果:
# <__main__.People object at 0x10813a320> # <__main__.People object at 0x10813a2e8> # <function People.test at 0x1081291e0> # <bound method People.test of <__main__.People object at 0x10813a320>> # <bound method People.test of <__main__.People object at 0x10813a2e8>>
原文地址:https://www.cnblogs.com/Bacardi/p/8335688.html
时间: 2024-10-07 23:07:31