`>>>和有什么不一样?some_object`和`>>>在Python解释器中打印some_object`? [英] What is the difference between `>>> some_object` and `>>> print some_object` in the Python interpreter?
问题描述
在解释器中,您可以只写一个对象的名称,例如像这样的解释器提示符处的列表 a = [1、2、3,uhellö"]
:
In the interpreter you can just write the name of an object e.g. a list a = [1, 2, 3, u"hellö"]
at the interpreter prompt like this:
>>> a
[1, 2, 3, u'hell\xf6']
或者您可以这样做:
>>> print a
[1, 2, 3, u'hell\xf6']
似乎等同于列表.目前,我正在使用hdf5来管理一些数据,并且我意识到上述两种方法之间存在差异.鉴于:
which seems equivalent for lists. At the moment I am working with hdf5 to manage some data and I realized that there is a difference between the two methods mentioned above. Given:
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")
print h5file
与
>>> h5file
所以我想知道是否有人可以解释这两种情况下Python的行为差异?
So I was wondering if someone could explain Python's behavioral differences in these two cases?
推荐答案
在终端中键入对象会调用 __ repr __()
,它用于详细显示要打印的对象(明确).当您说出要打印"的内容时,您正在调用 __ str __()
,因此要求提供人类可读的内容.
Typing an object into the terminal calls __repr__()
, which is for a detailed representation of the object you are printing (unambiguous). When you tell something to 'print', you are calling __str__()
and therefore asking for something human readable.
Alex Martelli在此处作了很好的解释.线程中的其他响应也可能说明差异.
Alex Martelli gave a great explanation here. Other responses in the thread might also illuminate the difference.
例如,看看日期时间对象.
For example, take a look at datetime objects.
>>> import datetime
>>> now = datetime.datetime.now()
比较...
>>> now
Out: datetime.datetime(2011, 8, 18, 15, 10, 29, 827606)
到...
>>> print now
Out: 2011-08-18 15:10:29.827606
希望这使它更加清晰!
这篇关于`>>>和有什么不一样?some_object`和`>>>在Python解释器中打印some_object`?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!