我正在解释器上测试一些代码,我注意到一些意外的行为sqlite3.Row http://docs.python.org/library/sqlite3.html#sqlite3.Row class.
我的理解是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>
I think sqlite3.Row
必须是一个子类tuple
,但我仍然不明白幕后到底发生了什么可能导致这种行为。谁能解释一下吗?
这是在 Python 2.5.1 上测试的,不确定其他 Python 版本的行为是否相同。
不确定这是否重要,但是row_factory http://docs.python.org/library/sqlite3.html#sqlite3.Connection.row_factory属性为我的Connection
被设置为sqlite3.Row
.
PySqlite 提供了特殊的本机钩子print
,但它没有实现__repr__
or __str__
。我想说这有点错过了机会,但至少它解释了您所观察到的行为。
查看 pysqlite 源码:https://github.com/ghaering/pysqlite/blob/master/src/row.c#L241 https://github.com/ghaering/pysqlite/blob/master/src/row.c#L241和Python文档:http://docs.python.org/c-api/typeobj.html#tp_print http://docs.python.org/c-api/typeobj.html#tp_print
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)