Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #108091

Use __repr__ to show the programmer's representation (was: Need help understanding list structure)

From Ben Finney <ben+python@benfinney.id.au>
Newsgroups comp.lang.python
Subject Use __repr__ to show the programmer's representation (was: Need help understanding list structure)
Date 2016-05-04 04:14 +1000
Message-ID <mailman.355.1462299295.32212.python-list@python.org> (permalink)
References (10 earlier) <d70720619f3142b9876e7d43507cd95f@seaexchmbx03.olympus.F5Net.com> <mailman.353.1462294382.32212.python-list@python.org> <221dcc70-39d8-4c9c-8827-8e0bc1ec1fda@googlegroups.com> <cdb6b67026d64546825afea8576bf4f0@seaexchmbx03.olympus.F5Net.com> <85inyvgga8.fsf_-_@benfinney.id.au>

Show all headers | View raw


Dan Strohl via Python-list <python-list@python.org> writes:

> One other point for you, if your "__repr__(self)" code is the same as
> the "__str__(self)" code (which it looks like it is, at a glance at
> least), you can instead reference the __str__ method and save having a
> duplicate code block...

Alternatively, consider: the ‘__repr__’ method is intended to return a
*programmer's* representation of the object. Commonly, this is text
which looks like the Python expression which would create an equal
instance::

    >>> foo = datetime.date.fromtimestamp(13012345678)
    >>> print(repr(foo))
    datetime.date(2382, 5, 7)

So if there is a sensible “here is the expression that could have been
used to create this instance” text, have the ‘__repr__’ method return
that text::

    >>> foo = LoremIpsum(bingle, bongle, bungle)
    >>> print(repr(foo))
    packagename.LoremIpsum("spam", 753, frob=True)

That text is very useful because it can be fed back into the interactive
interpreter to make an equal-valued instance and experiment further.

For some types, there isn't such an expression that would evaluate to an
equal-valued instance of the type. So the conventional non-evaluating
representation is used::

    >>> foo = frobnicate_the_widget(widget)
    >>> print(repr(foo))
    <LoremIpsum instance, foo: "spam" bar: 753>

This gives the crucial information of what the type is, and also gives
other interesting (to the programmer) attributes that characterise the
specific instance.

The fallback “<LoremIpsum instance at 0xDEADBEEF>” is the least helpful;
it gives the type and identity of the instance, but only because that's
the lowest common information ‘object’ can guarantee. Always implement a
more informative representation for your custom type, if you can.

-- 
 \        “Intellectual property is to the 21st century what the slave |
  `\                              trade was to the 16th.” —David Mertz |
_o__)                                                                  |
Ben Finney

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Need help understanding list structure moa47401@gmail.com - 2016-05-02 14:30 -0700
  Re: Need help understanding list structure Erik <python@lucidity.plus.com> - 2016-05-02 22:48 +0100
    Re: Need help understanding list structure moa47401@gmail.com - 2016-05-02 15:33 -0700
      Re: Need help understanding list structure Michael Torrie <torriem@gmail.com> - 2016-05-02 17:25 -0600
      Re: Need help understanding list structure Ben Finney <ben+python@benfinney.id.au> - 2016-05-03 09:43 +1000
        Re: Need help understanding list structure moa47401@gmail.com - 2016-05-03 06:21 -0700
          Re: Need help understanding list structure Chris Angelico <rosuav@gmail.com> - 2016-05-03 23:47 +1000
            Re: Need help understanding list structure moa47401@gmail.com - 2016-05-03 09:01 -0700
              RE: Need help understanding list structure Dan Strohl <D.Strohl@F5.com> - 2016-05-03 16:52 +0000
                Re: Need help understanding list structure moa47401@gmail.com - 2016-05-03 10:31 -0700
                RE: Need help understanding list structure Dan Strohl <D.Strohl@F5.com> - 2016-05-03 17:54 +0000
                Use __repr__ to show the programmer's representation (was: Need help understanding list structure) Ben Finney <ben+python@benfinney.id.au> - 2016-05-04 04:14 +1000
                RE: Use __repr__ to show the programmer's representation (was: Need help understanding list structure) Dan Strohl <D.Strohl@F5.com> - 2016-05-03 18:35 +0000
                Re: Use __repr__ to show the programmer's representation (was: Need help understanding list structure) moa47401@gmail.com - 2016-05-03 12:24 -0700
                Re: Use __repr__ to show the programmer's representation (was: Need help understanding list structure) Random832 <random832@fastmail.com> - 2016-05-03 15:37 -0400
                Re: Need help understanding list structure MRAB <python@mrabarnett.plus.com> - 2016-05-03 20:57 +0100
                Re: Use __repr__ to show the programmer's representation (was: Need help understanding list structure) Chris Angelico <rosuav@gmail.com> - 2016-05-04 09:40 +1000

csiph-web