cuốn sách gpt4 ai đã làm

python - __str__ 方法的问题

In lại Tác giả: Walker 123 更新时间:2023-11-28 19:40:44 26 4
mua khóa gpt4 Nike

这是我的脚本:

import math
class Vector:
def __init__(self, x=0.0, y=0.0):
self.x = x
self.y = y

def ___str___(self):
return "{0}, {1}".format(self.x, self.y)

@classmethod
def vectorPoints(cls, p1, p2):
a = p2[0] - p1[0]
b = p2[1] - p1[1]
return Vector(a, b)

A = (1,5)
B = (2,7)
vectAB = Vector.vectorPoints(A, B)
print(vectAB)
vect = Vector(1, 0)
print(vect)

当我运行这个脚本时,我得到:

<__main__.Vector object at 0x00FD5ED0>
<__main__.Vector object at 0x00FD5FF0>

rõ ràng __str__ 方法没有返回任何东西。

câu trả lời hay nhất

方法名应该是__str__(2个下划线包围),而不是___str___(3个下划线包围)。

关于python - __str__ 方法的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7340786/

26 4 0
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress