Python __repr__ and None

Patrizio Rullo picture Patrizio Rullo · Oct 13, 2011 · Viewed 9.8k times · Source

I'm quite new to Python and currently I need to have a __repr__ for a SqlAlchemy class. I have an integer column that can accept Null value and SqlAlchemy converts it to None. For example:

class Stats(Base):
   __tablename__ = "stats"
   description = Column(String(2000))
   mystat = Column(Integer, nullable=True)

What is the correct way to represent the "mystat" field in the __repr__ function when SqlAlchemy returns None?

Answer

yak picture yak · Oct 13, 2011

The __repr__ should return a string that describes the object. If possible, it should be a valid Python expression that evaluates to an equal object. This is true for built-in types like int or str:

>>> x = 'foo'
>>> eval(repr(x)) == x
True

If that's not possible, it should be a '<...>' string uniquely describing the object. The default __repr__ is an example of this:

>>> class Foo(object):
        pass
>>>
>>> repr(Foo())
'<__main__.Foo object at 0x02A74E50>'

It uses the object's address in memory to uniquely identify it. Of course address doesn't tell us much about the object so it's useful to override __repr__ and return a string describing the object's state.

The object's state is defined by other objects it contains so it makes sense to include their repr in yours. This is exactly what list or dict do:

>>> repr(['bar', Foo()])
"['bar', <__main__.Foo object at 0x02A74710>]"

In your case, the state is in your Column properties so you want to use their repr. You can use the %r formatting for this, it inserts a repr() of the argument:

def __repr__(self):
    return '<Stats: description=%r, mystat=%r>' % (self.description, self.mystat)

An equivalent using the new formatting:

def __repr__(self):
    return '<Stats: description={0.description!r}, mystat={0.mystat!r}>'.format(self)