Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #108106
| Path | csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail |
|---|---|
| From | Chris Angelico <rosuav@gmail.com> |
| Newsgroups | comp.lang.python |
| Subject | Re: Use __repr__ to show the programmer's representation (was: Need help understanding list structure) |
| Date | Wed, 4 May 2016 09:40:24 +1000 |
| Lines | 31 |
| Message-ID | <mailman.369.1462318828.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> <4c15c01e66314265b7251a16c3c3c235@seaexchmbx03.olympus.F5Net.com> <CAPTjJmriofjsu9TyuAkKGDEEQKARkkxMfvskX4nrVMg1-ESx-A@mail.gmail.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=UTF-8 |
| Content-Transfer-Encoding | quoted-printable |
| X-Trace | news.uni-berlin.de B4zoQR8n5SYbo2Udz+hcWQ7qKo22eanXmRoU8JWUjzLA== |
| Return-Path | <rosuav@gmail.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.004 |
| X-Spam-Evidence | '*H*': 0.99; '*S*': 0.00; 'subject:help': 0.07; 'cc:addr:python-list': 0.09; '"("': 0.09; 'objects.': 0.09; 'repr': 0.09; 'def': 0.13; 'wed,': 0.15; 'interpreter': 0.15; 'subject: \n ': 0.15; '2016': 0.16; '4:35': 0.16; 'col': 0.16; 'doing,': 0.16; 'eval': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'object).': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'repr()': 0.16; 'subject:programmer': 0.16; 'subject:show': 0.16; 'wrote:': 0.16; 'string': 0.17; 'case.': 0.18; "shouldn't": 0.18; '(not': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'saying': 0.22; 'libraries': 0.22; 'am,': 0.23; 'this:': 0.23; 'header:In-Reply- To:1': 0.24; 'subject:list': 0.26; 'message-id:@mail.gmail.com': 0.27; 'skip:( 20': 0.28; 'record': 0.29; 'dan': 0.29; 'prints': 0.29; 'subject:skip:u 10': 0.29; 'usable': 0.29; 'starts': 0.29; 'code': 0.30; 'skip:[ 10': 0.31; 'another': 0.32; 'skip:_ 10': 0.32; 'run': 0.33; 'done,': 0.33; 'list': 0.34; 'received:google.com': 0.35; 'could': 0.35; 'something': 0.35; 'but': 0.36; 'should': 0.36; 'there': 0.36; 'received:209.85': 0.36; 'subject:: ': 0.37; 'received:209.85.213': 0.37; 'received:209': 0.38; 'subject:the': 0.39; 'where': 0.40; 'still': 0.40; 'ever': 0.60; 'subject:Need': 0.61; 'back': 0.62; 'skip:n 10': 0.62; 'making': 0.62; 'needing': 0.63; 'effective': 0.63; 'python-list': 0.66; 'results': 0.66; 'worth': 0.67; 'hesitate': 0.71; 'chrisa': 0.84; "it'd": 0.84; 'to:none': 0.91; 'imagine': 0.96 |
| DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-transfer-encoding; bh=aU+A5E5R/xwVVyYAX1TuWmjOc7kvvBtZwHcTWEDzFN8=; b=b6gWxdHlknshJt0LB2FJUlTxkHs8ylWspESRjX1Snnvu7s7MjTOQnlnCZuR2ktxCql yLwwYc1N8pynob1GUK2JlLyg4k6WFWsLCTYe1xHG2VRCOKpAfsc5zLKEVmc9/8/nm7do SXt9E5p5CXuKWHES1bkbjmObZsqRt1M1iZeyne45s4SssfKMx668I4mDyXEJD35ENhoa fehvMKkwK05kvFlexbQRTWT19bb6k4xFsvUbvRZaxLWFGrIfZ3pInxlFK/nQDBWxwHvd jQvKHyPHBvb3g8IOB+vQdnh+blC6K1fmsQ0Vp5LmqWqmbDggMgZQ+JAxh+gY/mr7x6AZ mfuQ== |
| X-Google-DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:cc:content-transfer-encoding; bh=aU+A5E5R/xwVVyYAX1TuWmjOc7kvvBtZwHcTWEDzFN8=; b=XlfB/rh0PXjU9yRae+nJ3juCYKxl9e1aczSifiMXkp3QlDg112l12ztR8MaH27vLk/ M2lzX0Vy7Voc+SfNgXxWtSaROl34LWD08NlC+Poa/5LSYf7C9RuC6D7r8XvbaMtYyVqh WVxhpnG8pw8r4O95rXGMIcSphMK/1N2c87vKrVIOHUTl4Cwvpg2IN/TVsLymdRoZxSls n/EYvPRHPBGeBymCjOgkV4IVfppOeAunLkpG77JC8SJ7EKZF3SSSA/93vIZpKFCHJLIB 1fCECdPAxDvAsa6erDCdxG5T9/SKKF+CQt7sKmfEIeFCOaibiz2dclbCTUoTK6acTc6x JuNg== |
| X-Gm-Message-State | AOPr4FWeNIIW/ycmP8Lu4E9zKgktR5ARsZ5EGmwxjG0t/5XOHaYU81RD+Crqn8Yed0h+nmkfTWDHYxZ2rnyIZQ== |
| X-Received | by 10.50.221.169 with SMTP id qf9mr6882363igc.13.1462318824502; Tue, 03 May 2016 16:40:24 -0700 (PDT) |
| In-Reply-To | <4c15c01e66314265b7251a16c3c3c235@seaexchmbx03.olympus.F5Net.com> |
| 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 | <CAPTjJmriofjsu9TyuAkKGDEEQKARkkxMfvskX4nrVMg1-ESx-A@mail.gmail.com> |
| 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> <85inyvgga8.fsf_-_@benfinney.id.au> <4c15c01e66314265b7251a16c3c3c235@seaexchmbx03.olympus.F5Net.com> |
| Xref | csiph.com comp.lang.python:108106 |
Show key headers only | View raw
On Wed, May 4, 2016 at 4:35 AM, Dan Strohl via Python-list
<python-list@python.org> wrote:
> I also have never actually used repr() to create code that could be fed back to the interpreter (not saying it isn’t done, just that I haven’t run into needing it), and there are so many of the libraries that do not return a usable repr string that I would hesitate to even try it outside of a very narrow use case.
Here's a repr that I like using with SQLAlchemy:
def __repr__(self):
return (self.__class__.__name__ + "(" +
", ".join("%s=%r" % (col.name, getattr(self, col.name)) for
col in self.__table__.columns) +
")")
That results in something that *looks* like you could eval it, but you
shouldn't ever actually do that (because it'd create a new object).
It's still an effective way to make the repr readable; imagine a list
that prints out like this:
[Person(id=3, name="Fred"), Person(id=6, name="Barney"), Person(id=8,
name="Joe")]
You can tell exactly where one starts and another ends; you can read
what's going on with these record objects. Making them
"pseudo-evalable" is worth doing, even if you should never *actually*
eval them.
ChrisA
Back to comp.lang.python | Previous | Next — Previous 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