Classes
A class (classe, Python: class) bundles data with the behavior that operates on it. The constructor is named nouveau (Python: __init__) and receives the initial values. Inside methods, the current object is referred to as ce (Python: self).
123456789101112classe Etudiant:
fonction nouveau(nom, notes):
ce.nom = nom
ce.notes = notes
fonction moyenne():
retourner somme(ce.notes) / longueur(ce.notes)
# Créer une instance et appeler une méthode
etudiant = Etudiant("Aminata", [14, 16, 12])
afficher(etudiant.nom)
afficher(etudiant.moyenne())Aminata
14.0
You create an object by calling the class like a function: Etudiant("Aminata", [14, 16, 12]). Attributes (ce.nom, ce.notes) hold the state; methods (moyenne) are functions defined inside the class that act on ce.
Two landmarks are enough to read any Baobab class: nouveau = the constructor, ce = the current object. Everything else follows Python's object-oriented rules.