微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何将实例属性带入@staticmethod

如何解决如何将实例属性带入@staticmethod

我有一个基类,就像这样:

class coordinates(object):
    def __init__(self,x,y,z):
        self.x = x
        self.y = y
        self.z = z

    @property
    def x(self):
        return self.x

    @x.setter
    def x(self,x):
        self.x = x

    @property
    def y(self):
        return self.y

    @y.setter
    def y(self,y):
        self.y = y

    @property
    def z(self):
        return self.z

    @z.setter
    def z(self,z):
        self.z = z

然后我创建一个将从坐标继承的子类,并在其中包含一个静态方法,该方法将使用 x、y 和 z 等实例属性......像这样:

class volume(coordinates):
    def __init__(self,z):
        super().__init__(x,z)
        self.volume = self.calculate_volume()

    def calculate_volume(self):
        return self.x * self.y * self.z

    @staticmethod
    def goes_through(x,z,h,l):
        if x < l and y < h:
            return f"Use surface {x}{y} to go through"
        elif y < l and x < h:
            return f"Use surface {y}{x} to go through"
        elif x < l and z < h:
            return f"Use surface {x}{z} to go through"
        elif z < l and x < h:
            return f"Use surface {z}{x} to go through"
        elif z < l and y < h:
            return f"Use surface {z}{y} to go through"
        elif y < l and z < h:
            return f"Use surface {y}{z} to go through"
        else:
            return "Object can't go through"

然后我实例化一个对象并尝试获取它的体积并查看它是否通过以及如何通过:

obj1 = volume(100,200,400)
print(obj1.volume)
print(obj1.goes_through(obj1.x,obj1.y,obj1.z,350))

但我收到此错误

[上一行重复了 993 次以上] RecursionError: max 超出递归深度

非常感谢任何帮助。

解决方法

正确的 setter/getter 设置是:

class Obj(object):

    def __init__(self,value):
        self._value = value

    @property
    def value(self):
        return self._value

    @value.setter
    def value(self,value):
        self._value = value

但是您不需要这样做。在您的情况下,__init__ 函数就足够了。

,

问题在于您的 getter 和 setter 正在调用自己。当您在构造函数中执行 self.x = x 时,这会调用您的 x setter。然后在您的 x setter 中,您还执行 self.x = x。这将再次调用您的 x setter。这再次调用您的 x setter,依此类推,直到堆栈上的空间用完并且 Python 引发 RecursionError。

你可以去掉所有的 setter 和 getter,你的代码就可以正常工作了。

class coordinates(object):
    def __init__(self,x,y,z):
        self.x = x
        self.y = y
        self.z = z

或者,您可以使用不同的属性名称将数据存储在 getter 和 setter 中,这样它们就不会调用自己。

class coordinates(object):
    def __init__(self,z):
        self._x = x
        self._y = y
        self._z = z

    @property
    def x(self):
        return self._x

    @x.setter
    def x(self,x):
        self._x = x

    @property
    def y(self):
        return self._y

    @y.setter
    def y(self,y):
        self._y = y

    @property
    def z(self):
        return self._z

    @z.setter
    def z(self,z):
        self._z = z

但是,如果您没有特殊原因要做这样的事情,那么首先不使用 getter 和 setter 会更简单。

,

希望它可以在不使用 getter 或 setter 的情况下工作。

class coordinates(object):
    def __init__(self,z):
        self.x = x
        self.y = y
        self.z = z

class volume(coordinates):
    def __init__(self,z):
        super().__init__(x,z)
        self.volume = self.calculate_volume()

    def calculate_volume(self):
        return self.x * self.y * self.z

    @staticmethod
    def goes_through(x,z,h,l):
        if x < l and y < h:
            return f"Use surface {x}{y} to go through"
        elif y < l and x < h:
            return f"Use surface {y}{x} to go through"
        elif x < l and z < h:
            return f"Use surface {x}{z} to go through"
        elif z < l and x < h:
            return f"Use surface {z}{x} to go through"
        elif z < l and y < h:
            return f"Use surface {z}{y} to go through"
        elif y < l and z < h:
            return f"Use surface {y}{z} to go through"
        else:
            return "Object can't go through"


obj1 = volume(100,200,400)
print(obj1.volume)
print(obj1.goes_through(obj1.x,obj1.y,obj1.z,350))

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。