2

In an ipython terminal, suppose I create an object and then just type the object's name and hit return, what attributes/methods of that object (and in what order) are queried to produce the output that is returned to the screen?

E.g.,

In [1]: from mymodule import myclass
In [2]: C = myclass()
In [3]: C # hit return
Out[3]:

What attributes/methods of C are queried to produce the output in Out[3]?

Update: From the answer (and also a duplicate question that I found, it shows that __repr__ is called. However, I have a class that defines __repr__, but it doesn't appear to be being used and I get the following traceback error:

/usr/local/lib/python2.7/dist-packages/IPython/core/displayhook.pyc in __call__(self, result)
244             self.start_displayhook()
245             self.write_output_prompt()
--> 246             format_dict, md_dict = self.compute_format_data(result)
247             self.update_user_ns(result)
248             self.fill_exec_result(result)

/usr/local/lib/python2.7/dist-packages/IPython/core/displayhook.pyc in compute_format_data(self, result)
148 
149         """
--> 150         return self.shell.display_formatter.format(result)
151 
152     # This can be set to True by the write_output_prompt method in a subclass

/usr/local/lib/python2.7/dist-packages/IPython/core/formatters.pyc in format(self, obj, include, exclude)
150             return {}, {}
151 
--> 152         format_dict, md_dict = self.mimebundle_formatter(obj, include=include, exclude=exclude)
153 
154         if format_dict or md_dict:

<decorator-gen-12> in __call__(self, obj, include, exclude)

/usr/local/lib/python2.7/dist-packages/IPython/core/formatters.pyc in catch_format_error(method, self, *args, **kwargs)
215     """show traceback on failed format call"""
216     try:
--> 217         r = method(self, *args, **kwargs)
218     except NotImplementedError:
219         # don't warn on NotImplementedErrors

/usr/local/lib/python2.7/dist-packages/IPython/core/formatters.pyc in __call__(self, obj, include, exclude)
962                 return printer(obj)
963             # Finally look for special method names
--> 964             method = get_real_method(obj, self.print_method)
965 
966             if method is not None:

/usr/local/lib/python2.7/dist-packages/IPython/utils/dir2.pyc in get_real_method(obj, name)
 63 
 64     try:
---> 65         canary = getattr(obj, '_ipython_canary_method_should_not_exist_', None)
 66     except Exception:
 67         return None

where eventually it tries to use the __getattr__ method! In my class I have my own defined __getattr__ method, could this be causing a problem?

Matt Pitkin
  • 3,989
  • 1
  • 18
  • 32

1 Answers1

0

As the other answer, and e.g. the answer to this question, states in general the __repr__ method will get called.

However, ipython searches for other special versions of the __repr__ method to use when formatting the output, e.g., see this note to the format method of ipython's DisplayFormatter class:

If an object implement `_repr_mimebundle_` as well as various
`_repr_*_`, the data returned by `_repr_mimebundle_` will take
precedence and the corresponding `_repr_*_` for this mimetype will
not be called.

The formatter functions therefore check whether a particular method exists using the get_real_method() function (see here), which calls getattr using a dummy method name that should not exist. In my case I had defined my class to have its own __getattr__ method (and this is where my problems arose), which used the hasattr function. Due to issues with hasattr discussed here this causes problems, which in my case meant getting stuck in the __getattr__ function. So, the moral is be very careful if you define your own __getattr__ function as it can have unintended consequences!

Matt Pitkin
  • 3,989
  • 1
  • 18
  • 32