class变量如何在python中自动更新?

How can class variables be updated automatically in python?

我正在尝试自动更新处于固定关系中的 class 个变量。例如

class vector:
    def __init__(self, x, y, z):
        self.x = x
        self.y = y
        self.z = z
        self.r = (x**2+y**2+z**2)**0.5
        self.theta = tan((x**2+y**2)**0.5/z)
        self.phi = tan(y/x)

如果我更改此 class 实例的值 x,我想自动更新半径和角度。如果我改变角度或半径,我想自动更新 x、y、z 分量。有什么办法吗?

编辑: 好的,我现在有以下解决方案。如有错误或不符合常规的地方请指正。

from math import tan, cos, sin

class Vector:
    def __init__(self, x, y, z):
        self._x = x
        self._y = y
        self._z = z
        self._r = (self._x**2+self._y**2+self._z**2)**0.5
        self._theta = tan((self._x**2+self._y**2)**0.5/self._z)
        self._phi = tan(self._y/self._x)

    @property
    def x(self):
        return self._x
    @x.setter
    def x(self, new_x):
        self._x = new_x
        self._r = (self._x**2+self._y**2+self._z**2)**0.5
        self._theta = tan((self._x**2+self._y**2)**0.5/self._z)
        self._phi = tan(self._y/self._x)

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

    @y.setter
    def y(self, new_y):
        self._y = new_y
        self._r = (self._x**2+self._y**2+self._z**2)**0.5
        self._theta = tan((self._x**2+self._y**2)**0.5/self._z)
        self._phi = tan(self._y/self._x)

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

    @z.setter
    def z(self, new_z):
        self._z = new_z
        self._r = (self.x**2+self.y**2+self.z**2)**0.5
        self._theta = tan((self._x**2+self._y**2)**0.5/self._z)
        self._phi = tan(self._y/self._x)

    @property
    def r(self):
        return (self._x**2+self._y**2+self._z**2)**0.5

    @r.setter
    def r(self, new_r):
        self._r = new_r
        self._x = self._r*cos(self._theta)*cos(self._phi)
        self._y = self._r*cos(self._theta)*sin(self._phi)
        self._z = self._r*sin(self._theta)

    @property
    def theta(self):
        return tan((self._x**2+self._y**2)**0.5/self._z)

    @theta.setter
    def theta(self, new_theta):
        self._theta = new_theta
        self._x = self._r*cos(self._theta)*cos(self._phi)
        self._y = self._r*cos(self._theta)*sin(self._phi)
        self._z = self._r*sin(self._theta)

    @property
    def phi(self):
        return tan(self._y/self._x)

    @phi.setter
    def phi(self,new_phi):
        self._phi = new_phi
        self._x = self._r*cos(self._theta)*cos(self._phi)
        self._y = self._r*cos(self._theta)*sin(self._phi)
        self._z = self._r*sin(self._theta)

您正在寻找 @property 装饰器,它通过函数语句设置对象的变量。

from math import tan

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

    @property
    def r(self):
        return (self.x**2+self.y**2+self.z**2)**0.5

    @property
    def theta(self):
        return tan((self.x**2+self.y**2)**0.5/self.z)

    @property
    def phi(self):
        return tan(self.y/self.x)

因此,

v = Vector(1, 2, 3)
v.phi # -2.185039863261519

您可以重写您的 class 如下

import math

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

    @property
    def radius(self):
        return (self.x**2+self.y**2+self.z**2)**0.5

    @property
    def theta(self):
        return math.tan((self.x**2+self.y**2)**0.5/self.z)

    @property
    def phi(self):
        return math.tan(self.y/self.x)