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


Groups > comp.lang.python > #56744

Re: Unicode Objects in Tuples

From Roy Smith <roy@panix.com>
Newsgroups comp.lang.python
Subject Re: Unicode Objects in Tuples
Date 2013-10-12 09:11 -0400
Organization PANIX Public Access Internet and UNIX, NYC
Message-ID <roy-D76AC1.09110012102013@news.panix.com> (permalink)
References <CAP=-cKU6Xkt_rm4VYh7NfXLSfUDG7FZQXHz+U12gP6fbkdhxbQ@mail.gmail.com> <5257C35C.4070407@nedbatchelder.com> <CAP=-cKVJ10KmWCnc8-86KabXL=EmCSUJpO=jDVBpXmRPH1hHBg@mail.gmail.com> <CALwzidmtkaD9swOcEipvXSmeP-qooBSY=4Qd0NwRTMx9D5e7Rw@mail.gmail.com> <mailman.1034.1381575143.18130.python-list@python.org>

Show all headers | View raw


In article <mailman.1034.1381575143.18130.python-list@python.org>,
 Ned Batchelder <ned@nedbatchelder.com> wrote:

> This idea that the repr can reconstruct the object always fell flat with 
> me since the vast majority of classes don't have a repr that works that 
> way.  I look at it a little differently: the repr is meant to be as 
> unambiguous as possible to a developer.  It turns out that Python 
> literal syntax is really good at that, so where possible, that's what's 
> used.   But most classes don't make an attempt to create a Python 
> expression, because that's very difficult

Well, it's not actually difficult.  You could imagine doing something 
like:

def __repr__(self):
    return "pickle.loads(%s)" % pickle.dumps(self)

and while you can paste that into a REPL, it's not really useful to most 
people.

What we do with our database model layer is have the repr include the 
name of the class and the primary key.  So:

>>> str(u)
'roysmith'
>>> repr(u)
"<User 1000564: u'roysmith'>"

That follow's Ned's idea that reprs should be unambiguous.  In this 
case, if I want to reconstitute myself as an object, I can just do a 
database query for user_id = 1000564.

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


Thread

Re: Unicode Objects in Tuples Ned Batchelder <ned@nedbatchelder.com> - 2013-10-12 06:52 -0400
  Re: Unicode Objects in Tuples Roy Smith <roy@panix.com> - 2013-10-12 09:11 -0400

csiph-web