[英]Python - AttributeError: 'Particle' object has no attribute 'display'
[英]I get thrown a AttributeError: 'Particle' object has no attribute 'MomentumSum' , if someone could help that would be great, thanks
import numpy as np
import math
class Particle:
def __init__(self,position=np.array([0, 0, 0], dtype=float),velocity=np.array([0, 0, 0], dtype=float),acceleration=np.array([0, -10, 0], dtype=float),name='Ball',mass=1.0, G = 6.67408E-11):
self.position = np.array(position, dtype=float)
self.velocity = np.array(velocity, dtype=float)
self.acceleration = np.array(acceleration, dtype=float)
self.mass = mass
self.name = name
self.G = G
self.MomentumSum = np.array(MomentumSum, dtype=float)
def __str__(self):
return "Particle: {0}, Mass: {1:.3e}, Position: {2}, Velocity: {3}, Acceleration: {4}".format(
self.name, self.mass,self.position, self.velocity, self.acceleration
)
def updateGravitationalAcceleration(self, bodies):
self.acceleration = np.array([0,0,0], dtype = float)
for body in bodies:
if self.name != body.name:
distance = np.linalg.norm(self.position - body.position)
self.acceleration += ((-self.G*body.mass)/(distance**2))*((self.position-body.position)/distance)
def update(self,deltaT,method):
'Update method, takes in 3 arguemnts, self, time interval and method type e.g. 1 being Eulers'
if method == 1:
'Eulers Method'
self.position = self.position + self.velocity*(deltaT)
self.velocity = self.velocity + self.acceleration*(deltaT)
elif method ==2:
'Euler-Cromer Method'
self.velocity = self.velocity + self.acceleration*(deltaT)
self.position = self.position + self.velocity*(deltaT)
def kineticEnergy(self):
return 0.5*self.mass*(np.linalg.norm(self.velocity))**2
'''This is our problem class method'''
def MomentumOfBodies(self,bodies):
self.MomentumSum = np.array([0,0,0], dtype = float)
for body in bodies:
self.MomentumSum += body.mass * body.velocity
最后一種方法似乎是引發錯誤的方法,我檢查了縮進是否正確,對我來說一切似乎都很好,只是也許第二意見會顯示錯誤,提前謝謝!!
我用以下方法測試了您的 class:
import numpy as np
class Particle:
def __init__(self,position=np.array([0, 0, 0], dtype=float),velocity=np.array([0, 0, 0], dtype=float),acceleration=np.array([0, -10, 0], dtype=float),name='Ball',mass=1.0, G = 6.67408E-11):
self.position = np.array(position, dtype=float)
self.velocity = np.array(velocity, dtype=float)
self.acceleration = np.array(acceleration, dtype=float)
self.mass = mass
self.name = name
self.G = G
self.MomentumSum = np.array([1,2,3], dtype=float)
def __repr__(self):
return "Particle: {0}, Mass: {1:.3e}, Position: {2}, Velocity: {3}, Acceleration: {4}, Mo: {5}".format(
self.name, self.mass,self.position, self.velocity, self.acceleration, self.MomentumSum
)
parts = [Particle() for _ in range(4)]
print(parts)
print([p.MomentumSum for p in parts])
測試
303:~/mypy$ python3 stack65294899.py
[Particle: Ball, Mass: 1.000e+00, Position: [0. 0. 0.], Velocity: [0. 0. 0.], Acceleration: [ 0. -10. 0.], Mo: [1. 2. 3.], Particle: Ball, Mass: 1.000e+00, Position: [0. 0. 0.], Velocity: [0. 0. 0.], Acceleration: [ 0. -10. 0.], Mo: [1. 2. 3.], Particle: Ball, Mass: 1.000e+00, Position: [0. 0. 0.], Velocity: [0. 0. 0.], Acceleration: [ 0. -10. 0.], Mo: [1. 2. 3.], Particle: Ball, Mass: 1.000e+00, Position: [0. 0. 0.], Velocity: [0. 0. 0.], Acceleration: [ 0. -10. 0.], Mo: [1. 2. 3.]]
[array([1., 2., 3.]), array([1., 2., 3.]), array([1., 2., 3.]), array([1., 2., 3.])]
當遇到像你這樣的錯誤時,我喜歡退后一步檢查基礎知識,或者使用特殊腳本,或者使用交互式 session。 從你知道工作的部分構建代碼。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.