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


Groups > comp.lang.python > #108106

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

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 2016-05-04 09:40 +1000
Message-ID <mailman.369.1462318828.32212.python-list@python.org> (permalink)
References (12 earlier) <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>

Show all headers | 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 | NextPrevious 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