common.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. class Car:
  2. model = "BMW"
  3. engine = 1.6
  4. class Person:
  5. name = 'Ivan'
  6. age = 30
  7. def teset_attr():
  8. car = Car()
  9. print(type(car))
  10. print(isinstance(car, Car))
  11. print(Person.name)
  12. print(Person.__dict__)
  13. # Наличие атрибута
  14. print(hasattr(Person, 'sex'))
  15. # Получить атрибут
  16. print(getattr(Person, 'name'))
  17. Person.name = 'Misha'
  18. print(getattr(Person, 'name'))
  19. # Динамическое создание атрибутов
  20. Person.x = 200
  21. # Получить атрибут или значение если атрибута нет
  22. print(getattr(Person, 'x', 100))
  23. # Установить атрибут
  24. setattr(Person, 'y', 300)
  25. print(getattr(Person, 'y'))
  26. # Удалить атрибут
  27. del Person.y
  28. # Удалить атрибут
  29. delattr(Person, 'x')
  30. print(Person.z)
  31. def test_attr_2():
  32. class Empty:
  33. pass
  34. my_list = [
  35. ('apple', 23),
  36. ('banana', 80),
  37. ('cherry', 13),
  38. ('date', 10),
  39. ('elderberry', 4),
  40. ('fig', 65),
  41. ('grape', 5),
  42. ('honeydew', 7),
  43. ('kiwi', 1),
  44. ('lemon', 10),
  45. ]
  46. for i in my_list:
  47. setattr(Empty, i[0], i[1])
  48. print(Empty.__dict__)
  49. # print(Empty.__dict__())
  50. def test_attr_3(cl, name: str):
  51. return name in cl.__dict__
  52. def test_attr_4():
  53. class Person:
  54. name = "John Smith"
  55. age = 30
  56. gender = "male"
  57. address = "123 Main St"
  58. phone_number = "555-555-5555"
  59. email = "johnsmith@example.com"
  60. is_employed = True
  61. names = input().split()
  62. for i in names:
  63. print(i,'-', 'YES' if hasattr(Person, i.lower()) else 'NO', sep='')
  64. def main():
  65. # teset_attr()
  66. # test_attr_2()
  67. # print(test_attr_3(Car, 'model'))
  68. test_attr_4()
  69. if __name__ == '__main__':
  70. main()