Python __repr__ 和 None
我对 Python 还很陌生,目前我需要一个 SqlAlchemy 类的 __repr__ 。 我有一个可以接受 Null 值的整数列,SqlAlchemy 将其转换为 None 。 例如:
class Stats(Base):
__tablename__ = "stats"
description = Column(String(2000))
mystat = Column(Integer, nullable=True)
当 SqlAlchemy 返回 None
时,在 __repr__ 函数中表示“mystat”字段的正确方法是什么?
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
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
__repr__
应返回描述该对象的字符串。如果可能,它应该是一个计算结果为相等对象的有效 Python 表达式。对于int
或str
等内置类型来说也是如此:如果不可能,它应该是
'<...>' 唯一描述对象的字符串。默认的 __repr__ 就是一个例子:
它使用对象在内存中的地址来唯一标识它。当然,地址并不能告诉我们太多有关该对象的信息,因此重写 __repr__ 并返回描述对象状态的字符串很有用。
对象的状态由它包含的其他对象定义,因此将它们的
repr
包含在您的状态中是有意义的。这正是list
或dict
所做的:在您的情况下,状态位于您的
Column
属性中,因此您想使用它们的代表
。您可以为此使用%r
格式,它会插入参数的repr()
:使用新格式的等效项:
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 likeint
orstr
:If that's not possible, it should be a
'<...>'
string uniquely describing the object. The default__repr__
is an example of this: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 whatlist
ordict
do:In your case, the state is in your
Column
properties so you want to use theirrepr
. You can use the%r
formatting for this, it inserts arepr()
of the argument:An equivalent using the new formatting:
我试图找到一个可以在任何 SQLAlchemy 对象上使用的通用 __repr__ 方法,但只找到了这个页面。因此,我决定编写自己的代码,这就是我所做的:
这扩展了
Base
类以打印出特定实例的内容。因此,现在我创建的每个扩展 Base 的对象都将具有一个 __repr__ 方法,该方法不仅仅打印类名和实例哈希。编辑:我添加了一个
if __debug__
,因为此更改可能会导致您可能不希望在生产环境中泄漏的信息泄漏。我还添加了一个sorted
,以便显示保持一致。I was trying to find a generic
__repr__
method that could be used on any SQLAlchemy object and only found this page. So, I decided to write my own and here's what I've done:This extends the
Base
class to print out the contents of the particular instance. So, now every object I create that extends theBase
will have a__repr__
method that prints more than just the class name and the instance hash.EDIT: I added an
if __debug__
as this change may cause a leak of information you may not want leaked in a production environment. I also added asorted
so the display will be consistent.不是 sqlalchemy 的 generic_repr 装饰器-utils 提供适合您需求的基于社区的解决方案吗?
它将其保留为“无”。
Isn't the generic_repr decorator of sqlalchemy-utils providing a community based solution that suit your needs?
It leaves it as None.
也许
repr(mystat)
就是你想要的?maybe
repr(mystat)
is what you want?如果 mystat 是 None else mystat,则为“Null”
'Null' if mystat is None else mystat
前面的答案显示了如何覆盖 Base.__repr__ 正是我所需要的。谢谢你!这里它是用 Python 3.7+ 的 f 字符串重写的,并覆盖了 Flask-sqlalchemy
db.Model
。The previous answer showing how to override
Base.__repr__
was exactly what I needed. Thank you! Here it is re-written with f-strings for Python 3.7+ and overriding the flask-sqlalchemydb.Model
.