hasattr()
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
person = Person("Alice", 25)
print(hasattr(person, "name")) # Вывод: True
print(hasattr(person, "occupation")) # Вывод: Falseclass Person:
def __init__(self, name, age):
self.name = name
self.age = age
self.occupation = None
person = Person("Bob", 30)
if hasattr(person, "occupation"):
print(person.occupation)
else:
print("Occupation attribute does not exist")class Vehicle:
def start(self):
print("Starting the vehicle.")
car = Vehicle()
print(hasattr(car, "start")) # Вывод: True
print(hasattr(car, "stop")) # Вывод: FalseПоследнее обновление