跳转至

🔥AI副业赚钱星球

点击下面图片查看

郭震AI

39

编辑日期: 2024-11-28 文章阅读:

@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

上一个例子 下一个例子

京ICP备20031037号-1