← Back to team overview

launchpad-dev team mailing list archive

Give your models some repr love!

 

Just a quick plea:

When writing new model objects, or touching existing ones, please think about
given them a useful __repr__().  This can greatly improve the introspection
experience when using pdb or 'make harness'.

Many/most of our model classes have no repr which means when you look at an
object you get unhelpful output:

    >>> ls = getUtility(ILanguageSet)
    >>> ls.get(123)
    <Language at 0x12345678>

and then you have to find the class definition and go digging into various
attributes to figure out which object you have.  Wouldn't it be much nicer to
see:

    >>> ls.get(123)
    <Language: French (fr)>

with no further effort?

It's easy to do, too.  E.g. I have a branch hopefully landing soon that adds
this to class Language:

    def __repr__(self):
        return '<Language: %s>' % self.displayname

A word of warning though: adding a repr to existing classes can break doctests
far from the models you're modifying, so land such branches through ec2.

-Barry

Attachment: signature.asc
Description: PGP signature


Follow ups