python中的hasattr和getattr

主要参考Python的attr三兄弟(getattr、hasattr、setatter)

class Test:
   @property
   def name(self):
       return "Despair"

   @property
   def age(self):
       return 11


T = Test()
print(hasattr(T, 'name'))
print(getattr(T, 'name', None))
print(hasattr(T, 'age'))
print(getattr(T, 'age', None))
-----
True
Despair
True
11
发布了38 篇原创文章 · 获赞 3 · 访问量 2636

猜你喜欢

转载自blog.csdn.net/weixin_42828571/article/details/104048188