Nov-13-2019, 04:26 AM
I'm trying to make a class that makes dictionaries which have index system just like lists. The problem comes with __repr__, for some reason it returns nothing in PyCharm but it does in the Python interpreter.
Here is the code:
Here is the code:
class DictionnaireOrdonne:
"""Classe permettant de créer un dictionnaire ordonné comme une liste, avec des indices"""
liste_clefs = []
liste_valeurs = []
def __init__(self, **clefs_valeurs):
"""classe prennent un nombre indéfinit de paramètres nommées"""
self.clefs_valeurs = clefs_valeurs
self._dictionnaire = {}
def __repr__(self):
"""Cette méthode est appelée quand on appelle l'objet"""
return str(self.clefs_valeurs)
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)
testWhen I run it in PyCharm it returns:Output:Process finished with exit code 0But in the Python interpreter I get:Output:{'one' : 1, 'two' : 2}Someone please explain me why this is happening and how can I make it so that I get the same result from Python interpreter in PyCharm with only calling my object (test).
