Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #108091
| Path | csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail |
|---|---|
| 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 | Wed, 04 May 2016 04:14:39 +1000 |
| Lines | 50 |
| Message-ID | <mailman.355.1462299295.32212.python-list@python.org> (permalink) |
| References | <d6b0c524-f020-4726-b91a-517b2e8024ea@googlegroups.com> <5727CB31.5060309@lucidity.plus.com> <mailman.329.1462225716.32212.python-list@python.org> <ab5d3884-77ed-45d4-86ab-0333bf67dca1@googlegroups.com> <85mvo8gh57.fsf@benfinney.id.au> <mailman.332.1462232638.32212.python-list@python.org> <b93a85c8-042a-4c1f-800e-68a02dac3b78@googlegroups.com> <CAPTjJmoRxJSqAJNmWvxz9oNi294s=weAtM7yXC86OMYqq4jtjA@mail.gmail.com> <mailman.346.1462283241.32212.python-list@python.org> <db6035ff-7d16-4b21-92c8-c520a9b66230@googlegroups.com> <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> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=utf-8 |
| Content-Transfer-Encoding | 8bit |
| X-Trace | news.uni-berlin.de 11TW/IWmg1tCDRxYzCsX/Afh0NbFn2Ap0UpulrVQdLdA== |
| Cancel-Lock | sha1:QbJEw+6pWS6kLuL5OUG/S48f5WI= |
| Return-Path | <python-python-list@m.gmane.org> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.011 |
| X-Spam-Evidence | '*H*': 0.98; '*S*': 0.00; 'mertz': 0.05; 'attributes': 0.07; 'type,': 0.07; 'subject:help': 0.07; 'instance.': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'python': 0.10; 'interpreter': 0.15; 'commonly,': 0.16; 'fallback': 0.16; 'programmer)': 0.16; 'received:80.91.229.3': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'slave': 0.16; 'subject:programmer': 0.16; 'subject:show': 0.16; 'duplicate': 0.18; 'instance,': 0.18; '>>>': 0.20; 'object.': 0.22; 'text,': 0.22; 'header:User-Agent:1': 0.26; 'subject:list': 0.26; '(which': 0.26; 'header:X-Complaints-To:1': 0.26; 'least': 0.27; 'looks': 0.29; 'dan': 0.29; 'sensible': 0.29; 'subject:skip:u 10': 0.29; 'code': 0.30; 'implement': 0.32; 'skip:d 40': 0.32; 'point': 0.33; 'useful': 0.33; 'common': 0.33; 'foo': 0.33; 'equal': 0.34; 'gives': 0.35; 'could': 0.35; 'text': 0.35; 'identity': 0.35; 'instance': 0.35; "isn't": 0.35; 'skip:p 30': 0.35; 'but': 0.36; 'instead': 0.36; 'there': 0.36; 'to:addr :python-list': 0.36; 'subject:: ': 0.37; 'method': 0.37; 'received:org': 0.37; 'subject:the': 0.39; 'to:addr:python.org': 0.40; 'some': 0.40; 'save': 0.60; 'your': 0.60; 'subject:Need': 0.61; 'back': 0.62; 'skip:n 10': 0.62; 'more': 0.63; 'information': 0.63; 'python-list': 0.66; 'skip:\xe2 10': 0.70; '8bit%:43': 0.72; 'evaluate': 0.72; '21st': 0.76; '_o__)': 0.84; 'century': 0.84; 'experiment': 0.84; 'guarantee.': 0.84; 'received:125': 0.84; 'crucial': 0.91; 'glance': 0.91 |
| X-Injected-Via-Gmane | http://gmane.org/ |
| X-Gmane-NNTP-Posting-Host | jigong.madmonks.org |
| X-Public-Key-ID | 0xAC128405 |
| X-Public-Key-Fingerprint | 517C F14B B2F3 98B0 CB35 4855 B8B2 4C06 AC12 8405 |
| X-Public-Key-URL | http://www.benfinney.id.au/contact/bfinney-pubkey.asc |
| X-Post-From | Ben Finney <bignose+hates-spam@benfinney.id.au> |
| User-Agent | Gnus/5.13 (Gnus v5.13) Emacs/24.4 (gnu/linux) |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.22 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| X-Mailman-Original-Message-ID | <85inyvgga8.fsf_-_@benfinney.id.au> |
| X-Mailman-Original-References | <d6b0c524-f020-4726-b91a-517b2e8024ea@googlegroups.com> <5727CB31.5060309@lucidity.plus.com> <mailman.329.1462225716.32212.python-list@python.org> <ab5d3884-77ed-45d4-86ab-0333bf67dca1@googlegroups.com> <85mvo8gh57.fsf@benfinney.id.au> <mailman.332.1462232638.32212.python-list@python.org> <b93a85c8-042a-4c1f-800e-68a02dac3b78@googlegroups.com> <CAPTjJmoRxJSqAJNmWvxz9oNi294s=weAtM7yXC86OMYqq4jtjA@mail.gmail.com> <mailman.346.1462283241.32212.python-list@python.org> <db6035ff-7d16-4b21-92c8-c520a9b66230@googlegroups.com> <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> |
| Xref | csiph.com comp.lang.python:108091 |
Show key headers only | 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 | Next — Previous in thread | Next in thread | Find similar | Unroll 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