Python Forum
Why does pycharm return nothing when Python interpreter does?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why does pycharm return nothing when Python interpreter does?
#1
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:
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)
test
When I run it in PyCharm it returns:
Output:
Process finished with exit code 0
But 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).
Reply
#2
You need to use print(test) to get the desired output.
Any interactive shell is assuming this and automatically uses print() to produce output.
Pycharms just runs your code and just putting a variable on a line of code does not produce any output by definition.
Reply
#3
(Nov-13-2019, 06:20 AM)ThomasL Wrote: You need to use print(test) to get the desired output. Any interactive shell is assuming this and automatically uses print() to produce output. Pycharms just runs your code and just putting a variable on a line of code does not produce any output by definition.

Hello and thanks. Is there absolutely no way to get the desired output only calling test?
Reply
#4
No, because it isn't meant to work that way. How can the interpreter decide what things should or should not be printed? The interactive shell (or REPL, for read, evaluate, print, loop) is designed for well, evaluating expressions and displaying the results.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  filtering python code the interpreter reads Skaperen 0 1,149 Feb-02-2022, 08:29 PM
Last Post: Skaperen
  VS-Code or PyCharm Vs IDLE For Python Coding adt 14 10,108 Sep-01-2019, 05:17 AM
Last Post: adt
  Notepad++ with Python interpreter Truman 14 11,028 Feb-27-2019, 11:54 PM
Last Post: Truman
  clearing an interactive python interpreter/shell rootVIII 4 3,628 Jan-28-2019, 05:14 PM
Last Post: rootVIII
  Learn PyCharm and Python 3.6 Aug 2 15:00-16:00 GMT Larz60+ 0 2,703 Aug-01-2017, 07:51 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020