python-small-examples

@author jackzhenguo
@desc 
@date 2019/3/10

39 对象是否有某个属性

In [1]: class Student():
   ...:     def __init__(self,id,name):
   ...:         self.id = id
   ...:         self.name = name
   ...:     def __repr__(self):
   ...:         return 'id = '+self.id +', name = '+self.name

In [2]: xiaoming = Student(id='001',name='xiaoming')
In [3]: hasattr(xiaoming,'name')
Out[3]: True

In [4]: hasattr(xiaoming,'address')
Out[4]: False
[上一个例子](/python-small-examples/md/38.html) [下一个例子](/python-small-examples/md/40.html)