pythontips(2):hasattr的用法

  

class Xiaorui:
def __init__(self):
self.name = 'xiaorui'

def setName(self, name=''):
if name.strip():
self.name = name

def getName(self):
return self.name

def greet(self):
print("Hello, i'm %s" % self.name)

foo = Xiaorui()

if(hasattr(foo, 'setName')):
print(foo.setName())

if(hasattr(foo, 'getName')):
print(foo.getName())

if(hasattr(foo, 'setName')):
print(foo.setName('wcf'))

if(hasattr(foo, 'getName')):
print(foo.getName())
原文地址:https://www.cnblogs.com/aomi/p/6927328.html