Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Chris Angelico 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: References: <5727CB31.5060309@lucidity.plus.com> <85mvo8gh57.fsf@benfinney.id.au> <221dcc70-39d8-4c9c-8827-8e0bc1ec1fda@googlegroups.com> <85inyvgga8.fsf_-_@benfinney.id.au> <4c15c01e66314265b7251a16c3c3c235@seaexchmbx03.olympus.F5Net.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: 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: X-Mailman-Original-References: <5727CB31.5060309@lucidity.plus.com> <85mvo8gh57.fsf@benfinney.id.au> <221dcc70-39d8-4c9c-8827-8e0bc1ec1fda@googlegroups.com> <85inyvgga8.fsf_-_@benfinney.id.au> <4c15c01e66314265b7251a16c3c3c235@seaexchmbx03.olympus.F5Net.com> Xref: csiph.com comp.lang.python:108106 On Wed, May 4, 2016 at 4:35 AM, Dan Strohl via Python-list wrote: > I also have never actually used repr() to create code that could be fed b= ack to the interpreter (not saying it isn=E2=80=99t done, just that I haven= =E2=80=99t run into needing it), and there are so many of the libraries tha= t do not return a usable repr string that I would hesitate to even try it o= utside 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=3D%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=3D3, name=3D"Fred"), Person(id=3D6, name=3D"Barney"), Person(id= =3D8, name=3D"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