Skip to main content

hasattr

· One min read

如果对象具有给定的命名属性,则该hasattr()方法返回 true,否则返回 false。

例子

class Person:
age = 23
name = "Adam"

person = Person()

print("Person's age:", hasattr(person, "age"))
print("Person's salary:", hasattr(person, "salary"))

# Output:
# Person's age: True
# Person's salary: False