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
?
Python __repr__() function returns the object representation in string format. This method is called when repr() function is invoked on the object. If possible, the string returned should be a valid Python expression that can be used to reconstruct the object again.
Introduction to the Python __repr__ magic method The __repr__ method returns the string representation of an object. Typically, the __repr__() returns a string that can be executed and yield the same value as the object. In other words, if you pass the returned string of the object_name.
Python repr() The repr() function returns a printable representation of the given object.
Now if you go by the official python documentation – the __str__ is used to find the “informal”(readable) string representation of an object whereas __repr__ is used to find the “official” string representation of an object.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With