在下面的代码中,我不明白为什么x.__str__()
和str(x)
会产生不同的结果:
import gc
def mystr(self): return "{:.8f}".format(self)
underlying_dict = gc.get_referents(float.__dict__)[0]
underlying_dict["__repr__"] = mystr
underlying_dict["__str__"] = mystr
# I want to see 0.12345679
x = 0.123456789
print 1, "{:.8f}".format(x) # works
print 2, mystr(x) # works
print 3, x # fails
print 4, x.__str__() # works
print 5, x.__repr__() # works
print 6, str(x) # fails
print 7, type(x) # just checking the type
我把问题限制在比较
str(x)
和x.__str__()
在REPL(Python 2或Python 3)中:
使用IPython3:
但我在Jupyter笔记本上执行时遇到了和你一样的问题:
所以,这个问题似乎和朱庇特有关。但我还没找到更多的线索
相关问题 更多 >
编程相关推荐