在下面的代码块中,为什么print(new)语句调用str函数而不调用repr函数。是不是因为调用了print函数?
class Robot:
def __init__(self, name, build_year):
self.name = name
self.build_year = build_year
def __repr__(self):
return "Robot('" + self.name + "', " + str(self.build_year) + ")"
def __str__(self):
return "Name: " + self.name + ", Build Year: " + str(self.build_year)
if __name__ == "__main__":
x = Robot("Marvin", 1979)
print(str(x))
print(x)
print(repr(x))
new = eval(repr(x))
print(new)
answer is
Name: Marvin, Build Year: 1979
Name: Marvin, Build Year: 1979
Robot('Marvin', 1979)
Name: Marvin, Build Year: 1979发布于 2021-02-17 06:27:10
https://stackoverflow.com/questions/66232911
复制相似问题