我正在测试解释器上的一些代码,我注意到一些sqlite3.Row
类的意外行为。如何打印一个对象导致不同的输出比str()和repr()?
我的理解是print obj
总是会得到相同的结果print str(obj)
,并键入obj
到解释会得到相同的结果print repr(obj)
,但是这不是sqlite3.Row
情况:
>>> print row # the row object prints like a tuple
(u'string',)
>>> print str(row) # why wouldn't this match the output from above?
<sqlite3.Row object at 0xa19a450>
>>> row # usually this would be the repr for an object
(u'string',)
>>> print repr(row) # but repr(row) is something different as well!
<sqlite3.Row object at 0xa19a450>
我想sqlite3.Row
必须是tuple
的子类,但我仍然不确切了解可能导致此行为的幕后操作。任何人都可以解释吗?
这是在Python 2.5.1上测试的,不确定其他Python版本的行为是否相同。
不确定这是否重要,但我的Connection
的row_factory
属性设置为sqlite3.Row
。
发布之前,你看过http://stackoverflow.com/questions/1436703/difference-between-str-and-repr-in-python吗? – ktdrv
@kaloyan - 我找不到任何回答我的问题的东西,如果你能指点我的话。 –
有趣的行为。 'sqlite3.Row'似乎不是子类的元组,所以我的猜测是print语句的特例列表和/或基于除继承之外的其他标准的元组,但是我在文档中找不到任何可以承认的东西,更不用说解释了。 – millimoose