Auf*_*ind 6 python printing behavior
在解释器中,您只需a = [1, 2, 3, u"hellö"]在解释器提示符处写入对象的名称,例如列表,如下所示:
>>> a
[1, 2, 3, u'hell\xf6']
Run Code Online (Sandbox Code Playgroud)
或者你可以这样做:
>>> print a
[1, 2, 3, u'hell\xf6']
Run Code Online (Sandbox Code Playgroud)
这似乎相当于列表.目前我正在使用hdf5来管理一些数据,我意识到上面提到的两种方法之间存在差异.鉴于:
with tables.openFile("tutorial.h5", mode = "w", title = "Some Title") as h5file:
group = h5file.createGroup("/", 'node', 'Node information')
tables.table = h5file.createTable(group, 'readout', Node, "Readout example")
Run Code Online (Sandbox Code Playgroud)
的输出
print h5file
Run Code Online (Sandbox Code Playgroud)
不同于
>>> h5file
Run Code Online (Sandbox Code Playgroud)
所以我想知道是否有人可以解释Python在这两种情况下的行为差异?
小智 8
将对象键入终端调用__repr__(),用于详细表示要打印的对象(明确).当你说出"打印"的东西时,你正在打电话__str__(),因此要求一些人类可读的东西.
Alex Martelli 在这里做了很好的解释.线程中的其他响应也可能说明差异.
例如,看一下datetime对象.
>>> import datetime
>>> now = datetime.datetime.now()
Run Code Online (Sandbox Code Playgroud)
相比...
>>> now
Out: datetime.datetime(2011, 8, 18, 15, 10, 29, 827606)
Run Code Online (Sandbox Code Playgroud)
至...
>>> print now
Out: 2011-08-18 15:10:29.827606
Run Code Online (Sandbox Code Playgroud)
希望这会让它更清晰一点!
| 归档时间: |
|
| 查看次数: |
236 次 |
| 最近记录: |