阅读量

原创教程,严禁转载。引用本文,请署名 Python中文网, http://www.zglg.work


@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

上一个例子 下一个例子