如何向 init 之外的类添加属性

问题描述 投票:0回答:1

所以我开始研究一个类并定义属性。后来,我尝试在函数中使用其中一个属性,我得到了

AttributeError: type object 'Curve' has no attribute 'a'

这是相关代码和截图:

class Curve:

    def __init__(self, name: str, p: int, a: int, b: int, q: int, gx: int, gy: int, oid: bytes = None):
        self.name = name
        self.p = p
        self.a = a
        self.b = b
    def is_point_on_curve(self, point: (int, int)) -> bool:
        x, y, = point
        left = y * y
        right = (x * x * x) + (self.a * x) + self.b
        return (left - right) % self.p == 0

screenshot 该代码基于我在网上找到的椭圆曲线

我尝试在类的主体中添加属性(就像在 JAVA 中一样),但它不起作用。

python class oop attributeerror
1个回答
0
投票

我同意懒惰者的观点。您应该实例化 Curve 类的一个对象,然后您就可以访问它的属性和方法。

curve = Curve("mycurve", 2, 3, 4, 5, 6, 7)
x = curve.is_point_on_curve((2,2))
print(x)

我得到了

True

© www.soinside.com 2019 - 2024. All rights reserved.