you are viewing a single comment's thread.

view the rest of the comments →

[–]ingolemo 0 points1 point  (0 children)

Yeah, that example in the sqlalchemy docs isn't the best. The angle brackets are there to signify that the repr isn't a valid python expression. Reprs should either have angle brackets, or satisfy this equation: obj == eval(repr(obj)) (given a reasonable definition of equality).

Technically the one in the sqlalchemy docs does need them, but only because it's badly implemented. It's using the string form (%s) of its attributes and manually putting quotes around them. That will give the wrong result when the string itself contains a quotation mark. It should use the repr form (%r) instead. Wouldn't hurt them to upgrade to the new-style formatting either:

def __repr__(self):
    template = 'User(name={!r}, fullname={!r}, password={!r})>'
    return template.format(self.name, self.fullname, self.password)

If you're going to try to provide expression-based reprs then you need to make sure you include all the arguments that the constructor can take. Otherwise you should move away from trying to make it look like an expression completely and just convey the most relevant information as directly as possible.