12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- class Car:
- model = "BMW"
- engine = 1.6
- class Person:
- name = 'Ivan'
- age = 30
- def teset_attr():
- car = Car()
- print(type(car))
- print(isinstance(car, Car))
- print(Person.name)
- print(Person.__dict__)
- # Наличие атрибута
- print(hasattr(Person, 'sex'))
- # Получить атрибут
- print(getattr(Person, 'name'))
- Person.name = 'Misha'
- print(getattr(Person, 'name'))
- # Динамическое создание атрибутов
- Person.x = 200
- # Получить атрибут или значение если атрибута нет
- print(getattr(Person, 'x', 100))
- # Установить атрибут
- setattr(Person, 'y', 300)
- print(getattr(Person, 'y'))
- # Удалить атрибут
- del Person.y
- # Удалить атрибут
- delattr(Person, 'x')
- print(Person.z)
- def test_attr_2():
- class Empty:
- pass
- my_list = [
- ('apple', 23),
- ('banana', 80),
- ('cherry', 13),
- ('date', 10),
- ('elderberry', 4),
- ('fig', 65),
- ('grape', 5),
- ('honeydew', 7),
- ('kiwi', 1),
- ('lemon', 10),
- ]
- for i in my_list:
- setattr(Empty, i[0], i[1])
- print(Empty.__dict__)
- # print(Empty.__dict__())
- def test_attr_3(cl, name: str):
- return name in cl.__dict__
- def test_attr_4():
- class Person:
- name = "John Smith"
- age = 30
- gender = "male"
- address = "123 Main St"
- phone_number = "555-555-5555"
- email = "johnsmith@example.com"
- is_employed = True
- names = input().split()
-
- for i in names:
- print(i,'-', 'YES' if hasattr(Person, i.lower()) else 'NO', sep='')
- def main():
- # teset_attr()
- # test_attr_2()
- # print(test_attr_3(Car, 'model'))
- test_attr_4()
- if __name__ == '__main__':
- main()
|