You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

python-07.md 713 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. % Programmation avec Python (chapitre 7)
  2. % Dimitri Merejkowsky
  3. #
  4. \center \huge Rappels sur les classes
  5. # Définition d'une classe
  6. Construire la classe`Counter` avec un attribut `count`:
  7. ```python
  8. class Counter:
  9. def __init__(self):
  10. self.count = 0
  11. ```
  12. # Instantiation
  13. Construire une nouvelle *instance* de `Counter`
  14. ```python
  15. >>> counter = Counter()
  16. >>> counter.count
  17. 0
  18. ```
  19. # Méthode
  20. Ajouter une méthode pour incrémenter le compteur:
  21. ```python
  22. class Counter:
  23. def __init__(self):
  24. self.count = 0
  25. def increment(self):
  26. self.count += 1
  27. ```
  28. # Apeller une méthode
  29. ```python
  30. >>> counter = Counter()
  31. >>> counter.count
  32. 0
  33. >>> counter.increment()
  34. >>> counter.count
  35. 1
  36. ```