Printing `>&燃气轮机&燃气轮机;一些_object`和`>&燃气轮机&燃气轮机;在Python解释器中打印一些对象?

Printing `>&燃气轮机&燃气轮机;一些_object`和`>&燃气轮机&燃气轮机;在Python解释器中打印一些对象?,printing,behavior,python,Printing,Behavior,Python,在解释器中,您只需在解释器提示下写入对象的名称,例如列表a=[1,2,3,u“hellö”],如下所示: >>> a [1, 2, 3, u'hell\xf6'] 或者你可以: >>> print a [1, 2, 3, u'hell\xf6'] 这对于列表来说似乎是等效的。目前我正在使用hdf5管理一些数据,我意识到上面提到的两种方法之间存在差异。鉴于: with tables.openFile("tutorial.h5", mode = "w", ti

在解释器中,您只需在解释器提示下写入对象的名称,例如列表
a=[1,2,3,u“hellö”]
,如下所示:

>>> a
[1, 2, 3, u'hell\xf6']
或者你可以:

>>> print a
[1, 2, 3, u'hell\xf6']
这对于列表来说似乎是等效的。目前我正在使用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")
产量

print h5file
不同于

>>> h5file

所以我想知道是否有人能解释Python在这两种情况下的行为差异?

print语句总是调用
x.\uu str\uuuuuuuuuu()
方法,而(仅在交互式interpeter中)只调用对象调用的变量
x.\uuu repr\uuuuuuuuu()
方法

>>> '\x02agh'
'\x02agh'
>>> print '\x02agh'
'agh'

交互式解释器将打印输入其中的每个表达式的结果。(由于语句不求值,而是执行,因此此打印行为不适用于
print
本身、循环等语句。)

Niklas Rosenstein(使用2.6解释器)所述的交互式解释器使用
repr()
的证明:


因此,尽管交互式解释器中可能不需要
print
语句(除非您需要
str
而不是
repr
),但非交互式解释器不会这样做。将上述代码放在文件中并运行该文件将导致不打印任何内容。

在终端中键入对象将调用
\uuuu repr\uuu()
,这是用于打印对象的详细表示(明确)。当你告诉某些东西要“打印”时,你正在调用
\uuu str\uuu()
,因此要求一些人类可读的东西

亚历克斯·马泰利做了很好的解释。线程中的其他响应也可能说明这一差异

例如,看看datetime对象

>>> import datetime
>>> now = datetime.datetime.now()
比较

>>> now
Out: datetime.datetime(2011, 8, 18, 15, 10, 29, 827606)


希望这能让事情变得更清楚一点

查看以下位置的Python文档:

对象。repr(自)

(反向引号)来计算 对象如果可能的话,这应该看起来像一个有效的Python 表达式,该表达式可用于重新创建具有相同 值(给定适当的环境)。如果这是不可能的,请 应返回表单的字符串。 返回值必须是字符串对象。如果一个类定义了 repr()而不是str(),则在 该类实例的“非正式”字符串表示形式为 必需的

This is typically used for debugging, so it is important that the
表示是信息丰富且明确的

对象。str(自)

计算对象的“非正式”字符串表示形式。这 与repr()的不同之处在于它不必是有效的Python 表达式:可以使用更方便或简洁的表示 相反返回值必须是字符串对象

例如:

>>> class A():
...    def __repr__(self): return "repr!"
...    def __str__(self): return "str!"
... 
>>> a = A()
>>> a
repr!
>>> print(a)
str!
>>> class B():
...    def __repr__(self): return "repr!"
... 
>>> class C():
...    def __str__(self): return "str!"
... 
>>> b = B()
>>> b
repr!
>>> print(b)
repr!
>>> c = C()
>>> c
<__main__.C object at 0x7f7162efb590>
>>> print(c)
str!
对不起,英语不好:)。

print(variable)
等于
print(str(variable))

鉴于

variable
等于
print(repr(variable))


显然,对象的
\uuuu repr\uuuu
\uuuu str\uuuu
方法
h5文件
会产生不同的结果。

这是在哪里记录的?我找不到。@jtbandes不知道在哪里,但你可以自己测试一下。PS:有人能把内联代码格式化好吗?它在移动设备上不起作用。谢谢哈哈,我真的不知道在哪里,但肯定在文档中的某个地方。你的意思是
str(x)
repr(x)
,而不是
x.str()
x哈,真有趣。我编辑了答案,加入了
print x
语句,表明其中一个使用
str
,另一个使用
repr
,忘记了修改最后一段。对于无变量,键入变量不会打印任何内容。然而,
print(repr(None))
给出了
None
。怎么会?
Called by the repr() built-in function and by string conversions
This is typically used for debugging, so it is important that the
Called by the str() built-in function and by the print statement
>>> class A():
...    def __repr__(self): return "repr!"
...    def __str__(self): return "str!"
... 
>>> a = A()
>>> a
repr!
>>> print(a)
str!
>>> class B():
...    def __repr__(self): return "repr!"
... 
>>> class C():
...    def __str__(self): return "str!"
... 
>>> b = B()
>>> b
repr!
>>> print(b)
repr!
>>> c = C()
>>> c
<__main__.C object at 0x7f7162efb590>
>>> print(c)
str!
>>> a = datetime.time(16, 42, 3)
>>> repr(a)
'datetime.time(16, 42, 3)'
>>> str(a)
'16:42:03' #We dont know what it is could be just print:
>>> print("'16:42:03'")
'16:42:03'