Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
Este repositório está arquivado. Pode ver ficheiros e cloná-lo, mas não pode fazer envios ou lançar questões ou pedidos de integração.

python-09.md 2.4 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. % Programmation avec Python (chapitre 9)
  2. % Dimitri Merejkowsky
  3. \center \huge Rappels
  4. # Classes
  5. ```python
  6. class Car:
  7. total_number_of_cars = 0
  8. def __init__(self, color="black"):
  9. self.color = color
  10. Car.total_number_of_cars += 1
  11. def drive(self):
  12. print("vroom")
  13. @classmethod
  14. def print_number_of_cars(cls):
  15. print(cls.total_number_of_cars,
  16. "cars have been made")
  17. ```
  18. # Composition
  19. ```python
  20. class Authorization:
  21. def __init__(self, credentials_file):
  22. ...
  23. self.password = ...
  24. class Client:
  25. url = "https://exmple.com"
  26. def __init__(self, auth)
  27. self.auth = auth
  28. def make_request(self):
  29. password = self.auth.password
  30. requests.get(url, password=password)
  31. ```
  32. # Héritage - partage des attributs et méthodes
  33. ```python
  34. class A:
  35. def method_in_a(self):
  36. self.attribute_in_a = 42
  37. class B(A):
  38. def method_in_b(self):
  39. self.method_in_a() # ok
  40. self.attribute_in_a # ok
  41. ```
  42. # Héritage - ordre de résolution des méthodes
  43. ```python
  44. class A:
  45. def method_in_a(self):
  46. pass
  47. class B(A):
  48. def method_in_b(self):
  49. pass
  50. >>> a = A()
  51. >>> a.method_in_a() # ok
  52. >>> a.method_in_b() # error
  53. >>> b = B()
  54. >>> b.method_in_b() # ok
  55. >>> b.method_in_a() # ok
  56. ```
  57. # Héritage - ordre de résolution des méthodes
  58. ```python
  59. class A:
  60. def method_in_a(self):
  61. pass
  62. class B(A):
  63. def method_in_b(self):
  64. pass
  65. >>> a = A()
  66. >>> a.method_in_a() # ok
  67. >>> a.method_in_b() # error
  68. >>> b = B()
  69. >>> b.method_in_b() # ok
  70. >>> b.method_in_a() # ok
  71. ```
  72. # Héritage: surcharge
  73. ```python
  74. class A:
  75. def method(self):
  76. print("A!")
  77. class B(A):
  78. def method(self):
  79. print("B!")
  80. >>> a = A()
  81. >>> a.method() # ok
  82. 'A!'
  83. >>> b = B()
  84. >>> b.method()
  85. 'B!'
  86. ```
  87. # Héritage - super()
  88. ```python
  89. class A:
  90. def method(self):
  91. print("A!")
  92. class B(A):
  93. def method(self):
  94. super().method()
  95. print("B!")
  96. >>> a = A()
  97. >>> a.method() # ok
  98. 'A!'
  99. >>> b = B()
  100. >>> b.method()
  101. 'A!'
  102. 'B!'
  103. ```
  104. # Héritage - super() et \_\_init\_\_
  105. ```python
  106. # All animals have a species
  107. class Animal:
  108. def __init__(self, species):
  109. self.species = species
  110. # Pets are animals with a name
  111. class Pet(Animal):
  112. def __init__(self, species, name):
  113. super().__init__(species) # <- à ne pas oublier
  114. self.name = name
  115. # All dogs are pets
  116. class Dog(Pet):
  117. def __init__(self, name):
  118. super().init("dog", name)
  119. ```