Nov-18-2019, 02:07 AM
class DictionnaireOrdonne: """Classe permettant de créer un dictionnaire ordonné comme une liste, avec des indices""" def __init__(self, **keys_values): """classe prennent un nombre indéfinit de paramètres nommées""" self.keys_values = keys_values self._dictionnaire = self.keys_values for keys in self.keys_values: self.keys = keys def __repr__(self): """Cette méthode est appelée quand on appelle l'objet""" return str(self.keys_values) def __getitem__(self, key): """Cette méthode spéciale est appelée quand on fait objet[index] Elle redirige vers self._dictionnaire[index]""" return self._dictionnaire[key] def __setitem__(self, key, value): """Cette méthode est appelée quand on écrit objet[index] = valeur On redirige vers self._dictionnaire[index] = valeur""" self._dictionnaire[key] = value test = DictionnaireOrdonne(one=1, two=2, three=3) print(test.keys)When I run this code, I get:
Output:three
I only get one of the 3 keys, and it's the last one, i don't know why.However, when if I modify my for loop to this:
for keys in self.keys_values: print(keys)Then I get:
Output:one
two
three
I get the output wanted, but I can't use it because it will print itself without me calling it.Why do I get only get one key in my first example, and how can I make it so that I get my three keys when I call test.keys?