hasattr(object, name)
hasattr() 函数用于判断对象是否包含对应的属性。如果对象有该属性返回 True,否则返回 False。object -- 对象。name -- 字符串,属性名。
实例:

 1 #!/usr/bin/python
 2 # -*- coding: UTF-8 -*-
 3  
 4 class Coordinate:
 5     x = 10
 6     y = -5
 7     z = 0
 8  
 9 point1 = Coordinate() 
10 print(hasattr(point1, 'x'))
11 print(hasattr(point1, 'y'))
12 print(hasattr(point1, 'z'))
13 print(hasattr(point1, 'no'))  # 没有该属性

输出:

1 True
2 True
3 True
4 False

 


 
                    
            
                

相关文章:

  • 2022-12-23
  • 2019-08-16
  • 2022-01-12
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-09-19
猜你喜欢
  • 2022-12-23
  • 2021-12-07
  • 2022-02-18
  • 2022-12-23
  • 2021-09-10
  • 2021-08-30
  • 2022-12-23
相关资源
相似解决方案