-1

In IPython, typing the name of a class and hitting Enter displays the class's fully-qualified name. How do I programmatically access this name as a string? It's not the same as what str() and repr() return, and because this is a class, not an instance of that class, defining its magic methods __str__ and/or __repr__ wouldn't help.

Already been to this answer, but it's about the default Python interpreter, not IPython.

In [1]: class MyClass:
   ...:     pass
   ...: 

In [2]: MyClass
Out[2]: __main__.MyClass

In [3]: str(MyClass)
Out[3]: "<class '__main__.MyClass'>"

In [4]: repr(MyClass)
Out[4]: "<class '__main__.MyClass'>"

What method should I call on MyClass so I can get the same output when pressing enter on a class object?, __main__.MyClass, as a string?

As someone suggested this answer which I was already aware, but I am asking the question here is more curious about what internal method was called when hitting "enter" on a class object.

James Lin
  • 25,028
  • 36
  • 133
  • 233
  • 2
    The other answers are about the regular Python shell, which you aren't using. It looks like that's just showing you the fully-qualified name of the class, as used by the inherited `object` `__str__`, which is basically `f"{c.__module__}.{c.__qualname__}"` (see e.g. https://stackoverflow.com/q/2020014/3001761). – jonrsharpe Jul 05 '22 at 23:15
  • @G.Anderson I updated my question, the `c` is not an instance, but a class. I guess the `magic functions` don't apply? – James Lin Jul 05 '22 at 23:21
  • What interpreter are you using? – CrazyChucky Jul 05 '22 at 23:21
  • @CrazyChucky ipython – James Lin Jul 05 '22 at 23:22
  • What do you mean by "string value of a class"? Can you simiplify your example more to focus on the question at hand. You seem to have a bunch of extra stuff that isn't relevant to this question. – Code-Apprentice Jul 05 '22 at 23:26
  • sorry should have said `dot notation of the class` – James Lin Jul 05 '22 at 23:27
  • So you're asking not how to get the same output, but how the internal implementation of IPython, specifically, generates it? – CrazyChucky Jul 05 '22 at 23:51
  • 1
    `'.'.join((MyClass.__module__, MyClass.__name__))` ? – fsimonjetz Jul 05 '22 at 23:53
  • Maybe the answer I was looking for was could be: "It's just how ipython does it "magically" against a class object when entered" – James Lin Jul 05 '22 at 23:54
  • Well, "magically" is relative. But one would probably have to look at IPython's source code (or ask a developer) for a definitive answer. – CrazyChucky Jul 05 '22 at 23:55
  • 1
    `from IPython.display import display; display(MyClass)` seems to do what you want in an IPython shell, but that doesn't work in a regular shell when calling the same function. `sys.displayhook` as mentioned by Tim Peters in the post you linked to is probably the key (at least the [docs](https://ipython.org/ipython-doc/2/api/generated/IPython.core.displayhook.html) mention "a generalization of the `repr()` of an object". – fsimonjetz Jul 06 '22 at 00:04

1 Answers1

2

For any practical purpose, it's str(MyClass)[8:-2].

DYZ
  • 55,249
  • 10
  • 64
  • 93