Parent Class: class Body(object)
I have a parent class characterising the classical mechanics definition of a physical body. Such as it is, this parent class has the attributes: name, mass, position, velocity.
from Utilities import *
class Body(object):
'''
In classical mechanics a physical body is collection of
matter having properties including mass, velocity, momentum
and energy. The matter exists in a volume of three-dimensional
space called its extension.
'''
def __init__(self, name, mass):
if isinstance(name, str) and isinstance(mass, float):
#Name
self.name = name
#Mass
self.mass = mass
#Position record
self.positions = np.empty(shape = (0, 3), dtype = float)
#Velocity record
self.velocities = np.empty(shape = (0, 3), dtype = float)
pass
else:
raise TypeError('Name and mass must be string and float, respectivly.')
return None
Child Class: class Planet(Body)
Additionally, I have a child class characterising Planets, which are fundamentally physical bodies, and thus should inherit the abstract physical attributes of such, namely: name, mass, position, velocity.
class Planet(Body):
def __init__(self, name, mass = float):
self.mass = Planet_Mass(name)
Body.__init__(self, name, self.mass)
Usage
#Import the necesary modules
from context import Planet
#Instantiate Earth as a massive celestial object
Earth = Planet('Earth')
#Print the mass of the planet [10^24 kg]
print 'The mass of Earth is: ', Earth.mass, ' [10^24 kg]'
Result
The mass of Earth is: 5.97 [10^24 kg]
Problem
Fundamentally, all physical bodies have mass. However, in the context of this simulation, the method by which mass is determined differs between the various child classes of class Body(object), namely: class Planet(Body), class Satellite(Body), etc.
- In
class Planet(Body), mass is determined viaPlanet_Mass(name), which involves reading through a planetary fact sheet .txt file. - In
class Satellite(Body), mass is determined viaSatellite_Mass(name), which involves reading through a satellite fact sheet .txt file.
Is there anyway to alter the initialisation of my child classes to be less redundant?
Essentially, I would like to remove the necessity of stating self.mass = Planet_Mass(name) in class Planet(Body).
__init__with the calculated mass.mass = Planet_Mass(name)beforedef __init__(self, name, mass)? That will not work, asnamewill not be defined yet.