目前,当我display()
在 IPython 笔记本中使用函数时,我会在对象之间插入换行符:
>>> display('first line', 'second line')
first line
second line
但我想要print()
函数的行为,其中所有内容都保持在同一行,例如:
>>> print("all on", "one line")
all on one line
有没有改变display
行为的方法来做到这一点?
目前,当我display()
在 IPython 笔记本中使用函数时,我会在对象之间插入换行符:
>>> display('first line', 'second line')
first line
second line
但我想要print()
函数的行为,其中所有内容都保持在同一行,例如:
>>> print("all on", "one line")
all on one line
有没有改变display
行为的方法来做到这一点?
不,display
不能阻止换行,部分原因是没有要阻止的换行。每个显示的对象都有自己div
的位置,并且这些对象是垂直排列的。您可能可以通过使用 CSS 来调整它,但我不建议这样做。
真正让两个对象并排显示的唯一方法是构建自己的对象,该对象封装多个显示的对象,然后显示它。
例如,您的简单字符串案例:
class ListOfStrings(object):
def __init__(self, *strings):
self.strings = strings
def _repr_html_(self):
return ''.join( [
"<span class='listofstr'>%s</span>" % s
for s in self.strings
])
display(ListOfStrings("hi", "hello", "hello there"))