Python 如何获取和设置类的数据属性?
我想在Python中实现3D vector,将其组件存储在numpy数组或其他容器中。我想访问组件x,y和z,以便获取和设置它们。最好的方法是什么 我是这样实现的:Python 如何获取和设置类的数据属性?,python,python-3.x,numpy,Python,Python 3.x,Numpy,我想在Python中实现3D vector,将其组件存储在numpy数组或其他容器中。我想访问组件x,y和z,以便获取和设置它们。最好的方法是什么 我是这样实现的: import numpy as np class Vector3d: components = ['x', 'y', 'z'] def __init__(self): self._data = np.array([0.0, 0.0, 0.0]) def __getattr__(self,
import numpy as np
class Vector3d:
components = ['x', 'y', 'z']
def __init__(self):
self._data = np.array([0.0, 0.0, 0.0])
def __getattr__(self, key):
if key in self.components:
index = self.components.index(key)
return self._data[index]
else:
return super().__getattr__(key)
def __setattr__(self, key, value):
if key in self.components:
index = self.components.index(key)
self._data[index] = value
else:
return super().__setattr__(key, value)
def __repr__(self):
return repr(self._data)
def norm(self):
return np.linalg.norm(self._data)
a = Vector3d()
a.x = 1.2
a.y = 2.3
a.z = 3.4
print(a.x, a.y, a.z)
print(a)
print(a.norm())
我不喜欢里面的东西。首先,我复制了self.components:index=self.components.indexkey中的代码。第二,每次搜索索引对消耗时间来说似乎不是最优的。我相信有更好的方法来实现它。请告诉我你的方法
我正在寻找Python 3的解决方案。好的,注释部分似乎很狭窄,所以我将移动到这里 以下是我所做的,按优先级排序: 您不喜欢“键入self.components”线性搜索组件x或其他组件的方式。 您不喜欢重复的代码。 也许这是可行的:
import numpy as np
class Vector3d:
components = {'x':0, 'y':1, 'z':2}
def __init__(self):
self._data = [0.0, 0.0, 0.0]
def __getattr__(self, key):
return self._data[components[key]] ###!!!
def __setattr__(self, key, value):
self._data[components[key]] = value ###!!!
def __repr__(self):
return repr(self._data)
def norm(self):
return np.linalg.norm(self._data)
a = Vector3d()
a.x = 1.2
a.y = 2.3
a.z = 3.4
print(a.x, a.y, a.z)
print(a)
print(a.norm())
搜索现在不是线性的,所以它比你写的要优化一点。
多行重复代码现在已经不存在了,但这种自我。_data[components[key]]是我们必须接受的东西!:D
如果返回self.\u data[components[key]]失败,您可以添加一个try-and-catch从super访问数据 好的,评论部分似乎很狭窄,所以我将移动到这里 以下是我所做的,按优先级排序: 您不喜欢“键入self.components”线性搜索组件x或其他组件的方式。 您不喜欢重复的代码。 也许这是可行的:
import numpy as np
class Vector3d:
components = {'x':0, 'y':1, 'z':2}
def __init__(self):
self._data = [0.0, 0.0, 0.0]
def __getattr__(self, key):
return self._data[components[key]] ###!!!
def __setattr__(self, key, value):
self._data[components[key]] = value ###!!!
def __repr__(self):
return repr(self._data)
def norm(self):
return np.linalg.norm(self._data)
a = Vector3d()
a.x = 1.2
a.y = 2.3
a.z = 3.4
print(a.x, a.y, a.z)
print(a)
print(a.norm())
搜索现在不是线性的,所以它比你写的要优化一点。
多行重复代码现在已经不存在了,但这种自我。_data[components[key]]是我们必须接受的东西!:D
如果返回self.\u data[components[key]]失败,您可以添加一个try-and-catch从super访问数据 呃,为什么没有变量x,y和z,还有getter和setter函数呢?优化问题将不再是。。。你真的想让列表中的“x”、“y”和“z”变得如此通用吗?或者,为什么不使用一个字典,并进行一次try-catch,返回vec['x'],其中vec是替换组件的字典,“x”将作为值传递给getter/setter。可能有很多原因。例如,我可能希望将组件存储为ctypes.c_double*3数组,并将它们传递到DLL以获得某些计算的性能。或者将它们存储为numpy.array,可以访问所有numpy数组方法。但我仍然希望从外部通过x,y,z访问组件。你基本上是说,你需要某种数组。。。?你能用你可能使用的方法更新你的问题吗?因为如果要访问如上所示的a.x、a.y和a.z,我会创建变量x、y和z,当我想要得到向量时,我会有一个方法返回元组或列表中的x、y和z的值。请看。如果问题解决了,你可以通过接受解决方案将其标记为已解决!呃,为什么没有变量x,y和z,还有getter和setter函数呢?优化问题将不再是。。。你真的想让列表中的“x”、“y”和“z”变得如此通用吗?或者,为什么不使用一个字典,并进行一次try-catch,返回vec['x'],其中vec是替换组件的字典,“x”将作为值传递给getter/setter。可能有很多原因。例如,我可能希望将组件存储为ctypes.c_double*3数组,并将它们传递到DLL以获得某些计算的性能。或者将它们存储为numpy.array,可以访问所有numpy数组方法。但我仍然希望从外部通过x,y,z访问组件。你基本上是说,你需要某种数组。。。?你能用你可能使用的方法更新你的问题吗?因为如果要访问如上所示的a.x、a.y和a.z,我会创建变量x、y和z,当我想要得到向量时,我会有一个方法返回元组或列表中的x、y和z的值。请看。如果问题解决了,你可以通过接受解决方案将其标记为已解决!