【发布时间】:2019-03-22 09:18:39
【问题描述】:
我正在使用 Python OOP 解决n-body 模拟问题。我的每个粒子都是一个具有质量、位置、速度等属性的对象,我想每隔几个模拟时间将粒子的属性写入文件。因此,每个粒子都有一个文件,每一行在特定时间都有其属性。因此,例如,我的班级会是这样的,
from numpy import mod
class Particle(object):
def __init__(self):
self.idx = 3 # particle id
self.t = 0.7 # s
self.mass = 20. # kg
self.x = 20. # m
self.y = 30. # m
self.dt2store = 0.7 # s (simulated time)
def store(self):
if mod(self.t, self.dt2store):
self.writer()
def writer(self):
'''
It stores data in particle id3 file
data2store = [self.t, self.mass, self.x, self.y]
'''
最好、更有效的方法是什么?
感谢您的帮助。
【问题讨论】:
标签: python python-3.x file oop