hasattr/getattr/setattr/delattr

#通过字符串判断/获取/新增/删除对象属性或方法

class att(object):

  def __init__(self,var):

    self.var=var

  def func(self):

    print('%s is funcing'%self.var)

<1>hasattr:

  hasattr(att,'func') #True

  hasattr(att,'func1') #False

<2>getattr:

  att1=att('a')

  getattr(att1,'func')()#执行func函数

  getattr(att1,'var')#输出var属性

<3>setattr:

  att1=att('a')

  def func2(self):

    print('%s is func2'%self.name)

  setattr(att1,'fun2',func2)

  att1.fun2(att1)

<4>delattr:

  delattr(att1,'fun2')

05-06 18:12