方法有问题

2024-03-28 11:35:25 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我的剧本:

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)

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

^{pr2}$

显然,__str__方法没有返回任何内容。在


Tags: importselfreturndefmathclassprintvector