diff --git a/IPython/core/formatters.py b/IPython/core/formatters.py index fb09863..51553a1 100644 --- a/IPython/core/formatters.py +++ b/IPython/core/formatters.py @@ -343,8 +343,8 @@ class PlainTextFormatter(BaseFormatter): # something. enabled = Bool(True, config=False) - # Look for a __pretty__ methods to use for pretty printing. - print_method = Str('__pretty__') + # Look for a _repr_pretty_ methods to use for pretty printing. + print_method = Str('_repr_pretty_') # Whether to pretty-print or not. pprint = Bool(True, config=True) diff --git a/IPython/lib/pretty.py b/IPython/lib/pretty.py index 66ef7b7..dd721a8 100644 --- a/IPython/lib/pretty.py +++ b/IPython/lib/pretty.py @@ -29,12 +29,12 @@ The pretty library allows developers to add pretty printing rules for their own objects. This process is straightforward. All you have to do is to - add a `__pretty__` method to your object and call the methods on the + add a `_repr_pretty_` method to your object and call the methods on the pretty printer passed:: class MyObject(object): - def __pretty__(self, p, cycle): + def _repr_pretty_(self, p, cycle): ... Depending on the python version you want to support you have two @@ -42,13 +42,13 @@ compatibility one. - Here the example implementation of a `__pretty__` method for a list + Here the example implementation of a `_repr_pretty_` method for a list subclass for python 2.5 and higher (python 2.5 requires the with statement __future__ import):: class MyList(list): - def __pretty__(self, p, cycle): + def _repr_pretty_(self, p, cycle): if cycle: p.text('MyList(...)') else: @@ -75,7 +75,7 @@ class MyList(list): - def __pretty__(self, p, cycle): + def _repr_pretty_(self, p, cycle): if cycle: p.text('MyList(...)') else: @@ -164,7 +164,7 @@ class PrettyPrinter(_PrettyPrinterBase): """ Baseclass for the `RepresentationPrinter` prettyprinter that is used to generate pretty reprs of objects. Contrary to the `RepresentationPrinter` - this printer knows nothing about the default pprinters or the `__pretty__` + this printer knows nothing about the default pprinters or the `_repr_pretty_` callback method. """ @@ -330,14 +330,14 @@ class RepresentationPrinter(PrettyPrinter): self.begin_group() try: obj_class = getattr(obj, '__class__', None) or type(obj) - if hasattr(obj_class, '__pretty__'): - return obj_class.__pretty__(obj, self, cycle) + # First try to find registered singleton printers for the type. try: printer = self.singleton_pprinters[obj_id] except (TypeError, KeyError): pass else: return printer(obj, self, cycle) + # Next look for type_printers. for cls in _get_mro(obj_class): if cls in self.type_pprinters: return self.type_pprinters[cls](obj, self, cycle) @@ -345,6 +345,9 @@ class RepresentationPrinter(PrettyPrinter): printer = self._in_deferred_types(cls) if printer is not None: return printer(obj, self, cycle) + # Finally look for special method names. + if hasattr(obj_class, '_repr_pretty_'): + return obj_class._repr_pretty_(obj, self, cycle) return _default_pprint(obj, self, cycle) finally: self.end_group() diff --git a/docs/examples/core/display.py b/docs/examples/core/display.py index c460e70..404272c 100644 --- a/docs/examples/core/display.py +++ b/docs/examples/core/display.py @@ -14,8 +14,6 @@ class Circle(object): def _repr_pretty_(self, p, cycle): p.text(u"\u25CB") - __pretty__ = _repr_pretty_ - def _repr_html_(self): return "

Cirle: radius=%s

" % self.radius