- 判断对象是否有这个名字的属性,有就返回True,没有就返回False。name必须为字符串
hasaattr(object,name)
>>> if hasattr(p1, 'show'):
>>> print(getattr(p1, 'show'))
<bound method Point.show of <__main__.Point object at 0x0000014572376488>>
>>> if not hasattr(Point, 'add'):
>>> setattr(Point, 'add', lambda self, other: Point(self.x + other.x, self.y + other.y))
>>> print(Point.add)
<function <lambda> at 0x0000014572371558>
>>> print(p1.add)
<bound method <lambda> of <__main__.Point object at 0x0000014572376488>>
>>> print(p1.add(p2))
14 and 26
>>> if not hasattr(p1, 'sub'):
>>> setattr(p1, 'sub', lambda self, other: Point(self.x - other.x, self.y - other.y))
>>> print(p1.sub(p1, p2))
-6 and 6
>>> print(p1.__dict__)
{'x': 4, 'y': 16, 'z': 10, 'sub': <function <lambda> at 0x0000014572381DC8>}
>>> print(Point.__dict__)
{'__module__': '__main__', '__init__': <function Point.__init__ at 0x00000145722E04C8>, '__str__': <function Point.__str__ at 0x00000145722E0318>, 'sho