【问题标题】:Python: ways to store the attributes of an objectPython:存储对象属性的方法
【发布时间】:2023-01-07 04:41:19
【问题描述】:

考虑以下简化示例:

class Atom:

  def __init__(self, name_ID, position, mass, charge):  
    self.name_ID = name_ID # + date
    self.position, self.mass, self.charge = position, mass, charge

  def calculate_equation_of_motion(self):
    self.position, self.mass, self.charge = np.random.rand(3,3),np.random.rand(3,3),np.random.rand(3,3)

  def store_data(self):
    pass

At30 = Atom('test', 0,0,0)
At30.calculate_equation_of_motion()
#At30.store_data()

#At30_copy = load_object(name_ID='atom_30')
#print(At30_copy.charge)

一个包含 10 多个属性的类,这些属性由浮点数和数组参数和变量组成。

您有什么建议以我可以保存并反对稍后“加载”的方式来管理所有计算的数组?在这种情况下可能不清楚,但实际上,脚本需要一段时间来整合每个数组,所以我想使用一种简单的方法来保存信息。

【问题讨论】:

  • 其他语言将此称为“序列化”、“编组”等 - 本质上,以一种允许您将对象保存到文件或数据库然后稍后将其加载回来的方式表示对象。 Python 称之为“pickling”,there's a module for it that you may find helpful

标签: python database


【解决方案1】:

您尝试将其保存到平面文件中吗?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-06
    • 1970-01-01
    • 2014-03-04
    • 2013-07-08
    • 1970-01-01
    • 2021-07-18
    • 2021-04-18
    相关资源
    最近更新 更多